I'm trying to have a script read a text file, line by line, and then back up the directories which will be listed in each line. Obviously, I need to make sure that these are actually directories. So something like this seems to be a simple way to do it:
while read line; do
if [ -d "$line" ]; then
echo "This IS a path! - $line"
# backup script goes here
echo "This isn't a path: $line"
# do nothing
done < $FILE
The problem is it doesn't work. testfile.txt contains a load of gibberish and two valid paths, but this script doesn't recognise any of the lines as valid. I know the last two lines *are* valid, though, because when I type the following directly into a shell, it works:
if [ -d /some/valid/path/copied/from/the/text/file ]; then echo "IT'S A PATH"; else echo "nope"; fi;
I'm assuming this is some elementary error in the way I'm passing the contents of $line into the if statement, but I don't know what I'm doing wrong!