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