LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2005, 06:26 PM   #1
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Rep: Reputation: 15
null values in bash scripts always true?


In a shell script I am testing to see if a string passed to it is a file or a directory using the -f and -d oprerators something like this:

Code:
if [ -d $1 ] ; then
     echo $1 is a directory
else
     echo $1 is not a directory
fi
This works fine when I actually enter some text for $1, but if $1 is null (i.e no parameters were passed to the script) the script evaluates [ -d $1 ] as true. Shouldn't it be false??? The same thing happens with [ -f $1].

Why does bash think that an empty string is a directory or a file. It should be neither!

Does bash always evaluate a null value as true? Is that behavior common to all or most programming languages?

- Joe
 
Old 09-23-2005, 08:07 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's actually not bash that says whether the argument is a directory or not: it's test

The open square bracket is an alias for a separate system utility named test. That program is responsible for determining if an expression is true or false. If you wanted to verify, you can execute the same commands from the prompt:
Code:
$ test -d
$ echo $?
0
According to the test man page, 0 is true, 1 is false, and 2 indicates an error. I didn't see any explanation of what happens when an argument is absent, but I just skimmed the text. I'd have to look at the source code of test to determine why it spits out that value.

To answer your question though, normally null values and 0 are considered false in programming languages. So there is a little disagreement among shell/programming.
 
Old 09-24-2005, 02:18 AM   #3
wilmo2
LQ Newbie
 
Registered: Sep 2005
Distribution: Debian,Redhat,Gentoo,Suse,Slackware
Posts: 3

Rep: Reputation: 0
The fix to get around this problem would be to check for zero sized STRING.

$ man test

Code:
if [ -z $1 ] ; then  
    echo "Empty String" 
elif [ -d $1 ] ; then
     echo $1 is a directory
else
     echo $1 is not a directory
fi
P.S. Thanks Dark Helmet for the reference to $?, I learned something ...
 
Old 09-25-2005, 10:08 AM   #4
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by wilmo2
The fix to get around this problem would be to check for zero sized STRING.

$ man test

Code:
if [ -z $1 ] ; then  
    echo "Empty String" 
elif [ -d $1 ] ; then
     echo $1 is a directory
else
     echo $1 is not a directory
fi
P.S. Thanks Dark Helmet for the reference to $?, I learned something ...
No, the fix for this is to make sure you're giving the test an argument to test for being a directory:
Code:
if [ -d "$1" ]
then
    echo "$1 is a directory"
else
    echo "$1 isn't a directory"
fi
Don't be scared to use quotes, they're your friends so learn how to use them properly. You haven't quoted the argument to the -z test, so the quirk that makes -d return true is working for -z too - it's NOT testing the empty argument - it's not even seeing it if you don't quote it.
 
Old 09-26-2005, 11:57 AM   #5
jdupre
Member
 
Registered: Sep 2003
Distribution: LFS6.1, FC3, RH9
Posts: 36

Original Poster
Rep: Reputation: 15
Thank you eddiebaby. Quoting the varaible turns a null to an empty string and then I get the results I expected.
And a thank you to dark_helmet for pointing out "test".

- Joe
 
Old 09-27-2005, 05:22 PM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
It doesn't actually turn it into an empty string - it's that already. What the shell does is preserve it in place so the command can see it.
 
  


Reply



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
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
checking for null values from config file? zerointeger Programming 1 10-12-2005 11:29 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM
Why capitilize code? (ex. NULL, TRUE, etc) servnov Programming 9 01-08-2005 11:35 PM
bash - comparing a variable to several values davee Programming 3 05-05-2003 07:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 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
Open Source Consulting | Domain Registration