[SOLVED] Problems passing a string to if statement
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
Code:
#! /usr/bin/bash
#
FILE="testfile.txt"
while read line; do
if [ -d "$line" ]; then
echo "This IS a path! - $line"
# backup script goes here
else
echo "This isn't a path: $line"
# do nothing
fi
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:
Code:
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!
Sure - this is testfile.txt (I started off thinking I'd use hashes to comment out lines I didn't want, but then realised it was safer to treat any line as junk unless it specifically tested positive as a folder):
Code:
## How to use:
Line 1 - something
Line 2's got a quote mark
Line 3 has "double quotes"
Line 4 has a /slash
Line 5 blah blah blah.
this isn't a comment
## this is a comment
normal line.
blank line below:
/valid/path/number/one
/valid/path/number/two
The script doesn't produce any errors, but every line it reads produces the "this isn't a path" output.
This isn't a path: ## How to use:
This isn't a path:
This isn't a path: Line 1 - something
This isn't a path: Line 2's got a quote mark
This isn't a path: Line 3 has "double quotes"
This isn't a path: Line 4 has a /slash
This isn't a path: Line 5 blah blah blah.
This isn't a path: this isn't a comment
This isn't a path: ## this is a comment
This isn't a path: normal line.
This isn't a path: blank line below:
This isn't a path:
This isn't a path: /valid/path/number/one
This isn't a path: /valid/path/number/two
Are you sure those directories exist? I have just tried your script and it worked fine:
This isn't a path: ## How to use:
This isn't a path:
This isn't a path: Line 1 - something
This isn't a path: Line 2's got a quote mark
This isn't a path: Line 3 has "double quotes"
This isn't a path: Line 4 has a /slash
This isn't a path: Line 5 blah blah blah.
This isn't a path: this isn't a comment
This isn't a path: ## this is a comment
This isn't a path: normal line.
This isn't a path: blank line below:
This isn't a path:
This IS a path! - /tmp/test
If you are just testing that format makes sence as path, you can't use -d as it tests existence of directory
I've tried putting different paths in the text file:
Code:
[/tmp/temp] # cat testfile.txt
## How to use:
Line 1 - something
Line 2's got a quote mark
Line 3 has "double quotes"
Line 4 has a /slash
Line 5 blah blah blah.
this isn't a comment
## this is a comment
normal line.
blank line below:
/etc
/usr/bin
/tmp
[/tmp/temp] #
Output is:
Code:
This is not a path: ## How to use:
This is not a path:
This is not a path: Line 1 - something
This is not a path: Line 2's got a quote mark
This is not a path: Line 3 has "double quotes"
This is not a path: Line 4 has a /slash
This is not a path: Line 5 blah blah blah.
This is not a path: this isn't a comment
This is not a path: ## this is a comment
This is not a path: normal line.
This is not a path: blank line below:
This is not a path:
This is not a path: /etc
This is not a path: /usr/bin
This is not a path: /tmp
But (obviously) the last three do exist:
Code:
[/tmp/temp] # cd /etc
[/etc] # cd /usr/bin
[/usr/bin] # cd /tmp
[/tmp] #
(I've changed the echo statements slightly getting rid of exclamation marks and apostrophes in case they were causing any problems)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.