[ubuntu-za] Script error

Walter Leibbrandt walter+ubuntuza at translate.org.za
Tue Apr 14 14:43:40 BST 2009


Neil Muller wrote:
> That's because you're calling [ with only 1 argument, which returns true
> if the arguement is non-null (as required by POSIX).  Calling [ with 0
> arguments is always false. The -f only acts as an existance test if
> there are two arguments. 
>
> $ [ ] ; echo $? # always false
> 1
> $ [ aa ] ; echo $? # not null, so true
> 0
> $ [ -f ] ; echo $? # still not null
> 0
> $ unset UNDEFINED # UNDEFINED is null
> $ [ -f $UNDEFINED ] ; echo $? # still test for existance, even though null
> 1
> $ [ $UNDEFINED ] ; echo $? # false, because it's null
> 1
>
> The last can usefully be combined with the short-circuiting logic to
> test parameters, although the [ "x$VAR" = "xVal" ] version is shorter.
>
> Cryptic abuses of this logic left as an exercise for the reader.
Thanks for the very clear explanation, Neil. :)

-- 
Walter Leibbrandt                  Software Developer
Recent blogs:
* Firefox-style button with a pop-up menu
http://www.translate.org.za/blogs/walter/en/content/firefox-style-button-pop-menu





More information about the ubuntu-za mailing list