LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems passing a string to if statement (https://www.linuxquestions.org/questions/linux-newbie-8/problems-passing-a-string-to-if-statement-4175411393/)

rainjam 06-14-2012 04:43 AM

Problems passing a string to if statement
 
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!

Slackyman 06-14-2012 05:03 AM

Can you show me two or three lines from the textfile and say what kind of error the script produce?

rainjam 06-14-2012 05:10 AM

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.

rainjam 06-14-2012 05:43 AM

as in, this is the output:

Code:

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


epislav 06-14-2012 06:02 AM

Just delete double quotes in test statement:

if [ -d $line ]; then

rainjam 06-14-2012 06:05 AM

I tried that, but as the lines might contain spaces it gives me an error like this on virtually every line it reads -

Code:

readline_test.sh: line 9: [: too many arguments
... presumably it's expanding the string to something like

Code:

if [ -d ## How to use: ]; then
I've also tried

Code:

if [[ -d $line ]]; then
which doesn't throw any errors, but it does always go to the "this isn't a path" branch irrespective of whether $line is a path or not.

acid_kewpie 06-14-2012 06:14 AM

use [[ ]] definitely, as per your last comment.

It looks fine to me either way though, can you genuinely prove these paths DO exist? Just use "/etc" for example.

epislav 06-14-2012 06:14 AM

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

rainjam 06-14-2012 06:20 AM

hmm, I must be going mad!

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)

acid_kewpie 06-14-2012 06:23 AM

ok, is the file in unix format? Could there be a little ^M hiding at the end or something?

rainjam 06-14-2012 06:25 AM

You're a genius. It wasn't, I've changed it to Unix format and it now works. Thank you! :)


All times are GMT -5. The time now is 01:03 PM.