LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-14-2012, 05:43 AM   #1
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Rep: Reputation: Disabled
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!
 
Old 06-14-2012, 06:03 AM   #2
Slackyman
Member
 
Registered: Mar 2011
Location: Rome - Italy
Distribution: Slackware 13.1
Posts: 347

Rep: Reputation: 44
Can you show me two or three lines from the textfile and say what kind of error the script produce?
 
Old 06-14-2012, 06:10 AM   #3
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-14-2012, 06:43 AM   #4
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-14-2012, 07:02 AM   #5
epislav
LQ Newbie
 
Registered: Jun 2012
Posts: 20

Rep: Reputation: Disabled
Just delete double quotes in test statement:

if [ -d $line ]; then
 
Old 06-14-2012, 07:05 AM   #6
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-14-2012, 07:14 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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.
 
1 members found this post helpful.
Old 06-14-2012, 07:14 AM   #8
epislav
LQ Newbie
 
Registered: Jun 2012
Posts: 20

Rep: Reputation: Disabled
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

Last edited by epislav; 06-14-2012 at 07:16 AM.
 
1 members found this post helpful.
Old 06-14-2012, 07:20 AM   #9
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
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)
 
Old 06-14-2012, 07:23 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
ok, is the file in unix format? Could there be a little ^M hiding at the end or something?
 
1 members found this post helpful.
Old 06-14-2012, 07:25 AM   #11
rainjam
LQ Newbie
 
Registered: Jun 2012
Posts: 7

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] case statement passing multiple arguments supulton Programming 2 08-07-2010 09:00 PM
passing parameters in string in C shifter Programming 8 04-11-2008 10:01 AM
Question about passing using a function in an if statement. RHLinuxGUY Programming 7 09-04-2006 05:13 PM
a string switch statement in c++ pengu Programming 14 02-25-2006 10:58 PM
passing a string to a function jkobrien Programming 8 11-05-2003 02:41 PM


All times are GMT -5. The time now is 06:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration