Bacula-users

Re: [Bacula-users] Shell script question.

2010-03-04 04:39:30
Subject: Re: [Bacula-users] Shell script question.
From: Kevin Keane <subscription AT kkeane DOT com>
To: "bacula-users AT lists.sourceforge DOT net" <bacula-users AT lists.sourceforge DOT net>
Date: Thu, 4 Mar 2010 00:43:42 -0800
There are several reasons.

a) not all shells understand -z. Specifically, csh doesn't. Also, command.com 
and cmd.exe don't understand it. People used to any of these would naturally 
gravitate towards the alternative solution.

b) your version actually is incorrect. Even with -z, $var still needs to be 
enclosed in quotes. Otherwise, your statement would expand to either no or 
multiple arguments depending on the value of var and the shell you are using.

(empty var becomes no arg to -z)
var=
if [ -z ]; then

(multiple words in var become multiple args to -z)
var="this is a test"
if [ -z this is a test ]; then

(my shell, bash 3.2.39(1), handles var without quotes, but I don't think all 
versions do).

> -----Original Message-----
> From: Brian Debelius [mailto:bdebelius AT intelesyscorp DOT com]
> Sent: Wednesday, March 03, 2010 11:06 AM
> To: bacula-users AT lists.sourceforge DOT net; calug AT unknownlamer DOT org
> Subject: [Bacula-users] Shell script question.
> 
> Hi,
> 
>    Looking at Bacula scripts and other scripts, I see a test for an
> empty string performed by adding an 'x' before the variable, and then
> comparing this to another string that is just an 'x'.
> 
>    The shell test function has a -z string test that returns true if
> the
> string is empty.
> 
> It appears to me that more people use the 'x' comparison.  Is there a
> technical reason why one would be preferable to the other?
> 
>     Why would you do this:
> 
> if [ "x$var" = "x" ]; then
> ...
> fi
> 
>    Instead of this:
> 
> if [ -z $var ]; then
> ...
> fi


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Bacula-users mailing list
Bacula-users AT lists.sourceforge DOT net
https://lists.sourceforge.net/lists/listinfo/bacula-users

<Prev in Thread] Current Thread [Next in Thread>