When double quote is considered when not?

2011-03-30 Thread ali hagigat
The following scripts were run for /bin/bash, version 4.0.33, and then
comes their outputs. In the second example seems to have a warning:
"binary operator expected". Why the error is generated? and why there
is no error for the first example?
--
var1="word1 word2"
echo $var1
if (test -z "\"$var1\"")   then
echo "first"
else
echo second
fi

word1 word2
second
--
var1="word1 word2"
echo $var1
if (test -z ""$var1"")   then
echo "first"
else
echo second
fi

word1 word2
./ss1: line 3: test: word1: binary operator expected
second
--



Re: When double quote is considered when not?

2011-03-30 Thread Davide Brini
On Wednesday 30 Mar 2011 11:13:58 ali hagigat wrote:

> The following scripts were run for /bin/bash, version 4.0.33, and then
> comes their outputs. In the second example seems to have a warning:
> "binary operator expected". Why the error is generated? and why there
> is no error for the first example?
> --
> var1="word1 word2"
> echo $var1
> if (test -z "\"$var1\"")   then
> echo "first"
> else
> echo second
> fi
> 
> word1 word2
> second
> --
> var1="word1 word2"
> echo $var1
> if (test -z ""$var1"")   then
> echo "first"
> else
> echo second
> fi
> 
> word1 word2
> ./ss1: line 3: test: word1: binary operator expected
> second
> --

Use "set -x" at the beginning of the script and rerun it, and you'll 
immediately see why.

-- 
D.



Re: When double quote is considered when not?

2011-03-30 Thread Mart Frauenlob

On 30.03.2011 12:13, ali hagigat wrote:

The following scripts were run for /bin/bash, version 4.0.33, and then
comes their outputs. In the second example seems to have a warning:
"binary operator expected". Why the error is generated? and why there
is no error for the first example?
--
var1="word1 word2"
echo $var1
if (test -z "\"$var1\"")   then


why run the test in a subshell?
is there any reason for the second quote pair?


echo "first"
else
echo second
fi

word1 word2
second
--
var1="word1 word2"
echo $var1
if (test -z ""$var1"")   then


"" expands to nothing, as nothing in between opening and closing quote.
$var1 (now unqoted) expands to 'word1 word2', so the test command sees 
'word2' while it expects another operator.



echo "first"
else
echo second
fi

word1 word2
./ss1: line 3: test: word1: binary operator expected
second
--







Re: When double quote is considered when not?

2011-03-30 Thread Greg Wooledge
On Wed, Mar 30, 2011 at 02:43:58PM +0430, ali hagigat wrote:
> --
> var1="word1 word2"
> echo $var1
> if (test -z "\"$var1\"")   then
> echo "first"
> else
> echo second
> fi

Problems in this script:

 * Unquoted $var1 on line 2.  This means that instead of passing the actual
   content of the variable to echo, bash is going to split it into words,
   perform globbing (filename expansion) on each of those words, and then
   pass the resulting lists of words to echo.

 * Unnecessary subshell invocation (parentheses) on line 3.  There's
   no reason to create a new process there -- all it does is slow down
   the script.

 * You are testing the length of a string that will NEVER be zero-length.
   The string you are testing has two literal quotation marks in it, so
   it will ALWAYS be at least 2 characters long.  The "test -z" will
   therefore never be true.

> --
> var1="word1 word2"
> echo $var1
> if (test -z ""$var1"")   then
> echo "first"
> else
> echo second
> fi

Problems in this script:

 * Unquoted $var1 on line 2, same as before.

 * Unquoted $var1 on line 3.  This is even worse than the case on line 2,
   because when you pass multiple arguments to the test command following
   a -z switch, you will generate an error condition.

 * Unnecessary subshell, just as before.

 * Unnecessary use of double double quotes on line 3.  All they are doing
   is increasing the length of the code.  They have no effect.  $var1
   and ""$var1"" and $var1 are all the same thing.  Adding
   zero-length strings to a string does not change the string, no matter
   how many of them you add.

Here is what you SHOULD have done:

var1="what   ever"
echo "$var1"
if test -z "$var1"; then
  echo empty
else
  echo not empty
fi



Re: When double quote is considered when not?

2011-03-30 Thread Mart Frauenlob

On 30.03.2011 14:09, Mart Frauenlob wrote:
[...]

 so the test command sees

'word2' while it expects another operator.


sorry, i meant to write 'word1'