[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Shell script question



Hi,
 
What I want is this: I have a file in which a list of files or directories are present, like below (let's name this file input_file):
 
/mnt/hda6/File1
/mnt/hda5/Directory1
/mnt/hda6/File2
/mnt/hda6/File3
/mnt/hda6/Directory2
...
 
In a shell script I want to read this file line by line and depending on the type of entry, directory or file, I want to take some action. Because file and directory names may contain spaces, I cannot do it like this:
 
ENTRY=$(cat input_file)
 
for I in $ENTRY
do
...
done
 
I found a solution for this:
 
LINES=$(wc -l input_file)
while [ $LINES -gt 0 ]
do
  ENTRY=$(sed -e '1q' input_file)
  #do smth
  sed -e '2,$w input_file' input_file
  LINES=$(wc -l input_file)
done

By this I memorise the first line and delete the first line from input_file. But I don't like this solution because it writes the file each time in the loop. Could suggest anyone a more elegant solution.


Reply to: