[CentOS] C5 BASH IF

Sat Feb 14 09:27:57 UTC 2015
J Martin Rushton <martinrushton56 at btinternet.com>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try using [[ and ]] rather than [ and ].  Under BASH the double form
are reserved words with special meaning to the shell whereas the
single form are just a synonym for test.  Parsing and splitting rules
are different, in particular word splitting and pathname expansion are
not performed.  Consider:

#!/bin/sh
myvar=""
set -vx
[[ -n $myvar ]] && echo "non-null"
[ -n $myvar ] && echo "non-null"

bash-4.2$ ./X
[[ -n $myvar ]] && echo "non-null"
+ [[ -n '' ]]
[ -n $myvar ] && echo "non-null"
+ '[' -n ']'
+ echo non-null
non-null
bash-4.2$

Note how in the second case $myvar has been parsed out of existence!

On 14/02/15 05:54, Always Learning wrote:
> 
> On Fri, 2015-02-13 at 23:46 -0600, Les Mikesell wrote:
> 
>> I think you are missing some very basic concepts here.  First, 
>> the shell likes to parse things separated by white space. Second,
>> [ is a synonym for test which is a build-in version of /bin/test,
>> so try 'man test' for the syntax of tests.   And third, you
>> generally should use double quotes around variables in tests so
>> they continue to exist as an empty string if the variable happens
>> to not be set.
> 
> Thanks for that.  I assumed if test 1 worked, so would test 2.
> 
> Have re-run test 2 with
> 
> 
>> 16 if  [ $file = "law00css" ] 17   then 18        echo $file 19 
>> echo "css" 20 else 21        echo "no css" 22 fi
> 
> and got
> 
>> + '[' law45p07a01 = law00css ']' + echo 'no css' no css + exit
> 
> which is correct (for the first time). It seems that following your
> good advice and plonking spaces around the = has solved the 
> problem.
> 
> Thank you very much. Now I can go to bed a satisfied person :-)
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBAgAGBQJU3xUdAAoJEAF3yXsqtyBlOHYQAN6KUdCq8YL+CIn+qTtFI++W
mm44gmikluttmHl+llH17jaaaznakhZ+RmFjtygaHCSPZxxHZB9y5Dwv7czOaGpP
ECbsCGUSHRa/yDZNcTy/5deXCta+mEeUkx105u6Usou+DA9XCYXqvWpNDPopNRJF
Y4mwtgH7Jx2EgILgauECJFnuTtzHcZ2ps88d7H1nkJl49Q0EfmSPbcFrOoTJFeyM
Qs2nl7OpPCQh/nNAuVW3S+aiYF+7Oe++tx3MaPYI6Y5GUhoAKmfM/Ab58iT+4ckV
ha8tFY5cbCIPhKEzOTGK7rbEhmae9jgBl6s3HqaWiMrNPVrsKw/OLz0/V0j8S8Ib
d2AOKJBaVYAZgYOW7yI9vhzO+zdUnhHXHHx9hT5kk7Jg5YI3nhMfotdVpOwZgu3i
DewEa4bm4tR13ku54lYqmG1ehxyjxDUtPCF9DKyXriViGT+mpX7go4A3XPmnEk1n
K48gQOHA4q4t1DTv6t3ATP3TkBlXWG0NzHprw9wB0LKgv00//9AJ5me+Mtwx8GTi
g9Vg4jHH0EPu3WBBRnI5H3hFHRh/NtpSsEl0dGST5SpAUnuQikE6LXMmn1gw9bfa
8jsHL4mi/c70ugi4GQJyrhuZ/YIcIC6JSpugfRp7iqadJ2QiB4TmcgZl9R+7eAYu
4sK0nBvbVxh6ZQVrRhL0
=2980
-----END PGP SIGNATURE-----