LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   null values in bash scripts always true? (https://www.linuxquestions.org/questions/programming-9/null-values-in-bash-scripts-always-true-366390/)

jdupre 09-23-2005 06:26 PM

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

Dark_Helmet 09-23-2005 08:07 PM

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.

wilmo2 09-24-2005 02:18 AM

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 ... :)

eddiebaby1023 09-25-2005 10:08 AM

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.

jdupre 09-26-2005 11:57 AM

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

eddiebaby1023 09-27-2005 05:22 PM

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.


All times are GMT -5. The time now is 10:00 AM.