Being new to some aspects of BASH, I tried to reduce the quantity of scripts by introducing a comparison test into an existing working script. The script refused to work until I placed [ ] around the actual test. The second test, in the same script, misfunctioned until I removed the [ ] around the second test. ---------------------------- NON WORKING first comparison 5 if $dir="law" 6 then 7 www="law" 8 dir=${file:0:5} 9 else 10 www="www/s" 11 fi Error message = /root/bin/.us: line 5: law=law: command not found --------------------------------- WORKING first comparison 5 if [ $dir="law" ] 6 then 7 www="law" 8 dir=${file:0:5} 9 else 10 www="www/s" 11 fi ------------------------------------ NON-WORKING second comparison 15 if [ $file='law00.css' ] 16 then 17 file=$dir/$file 18 echo "css" 19 else 20 file=$dir/$file\.php 21 echo "no css" 22 fi 23 #---------------------------- Every comparison in the second test, including wrong comparisons, satisfy the test and caused the 'css' display. When line 15 was changed to 15 if [ $file='law00css' ] everything continued to match including items with no 'css' in the file name. Baffled. Are C5 BASH scripts restricted to only 1 IF comparison ? -- Regards, Paul. England, EU. Je suis Charlie.
On Fri, Feb 13, 2015 at 11:26 PM, Always Learning <centos at u64.u22.net> wrote:> Being new to some aspects of BASH, I tried to reduce the quantity of > scripts by introducing a comparison test into an existing working > script. > > The script refused to work until I placed [ ] around the actual test. > The second test, in the same script, misfunctioned until I removed the > [ ] around the second test. >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. -- Les Mikesell lesmikesell at gmail.com
On Sat, 2015-02-14 at 05:26 +0000, Always Learning wrote:> NON-WORKING second comparison > > 15 if [ $file='law00.css' ] > 16 then > 17 file=$dir/$file > 18 echo "css" > 19 else > 20 file=$dir/$file\.php > 21 echo "no css" > 22 fi > 23 #---------------------------- > > Every comparison in the second test, including wrong comparisons, > satisfy the test and caused the 'css' display. > > When line 15 was changed to > > 15 if [ $file='law00css' ] > > everything continued to match including items with no 'css' in the file > name.I re-ran the script with 'set -x' for 16 if [ $file='law00css' ] 17 then 18 echo $file 19 echo "css" 20 else 21 echo "no css" 22 fi and received:- + '[' law45p07a01=law00css ']' + echo law45p07a01 law45p07a01 + echo css css -- Regards, Paul. England, EU. Je suis Charlie.
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 fiand got> + '[' law45p07a01 = law00css ']' > + echo 'no css' > no css > + exitwhich 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 :-) -- Regards, Paul. England, EU. Je suis Charlie.
On 02/13/2015 11:47 PM, Always Learning wrote:> I re-ran the script with 'set -x' for > > 16 if [ $file='law00css' ] > 17 then > 18 echo $file > 19 echo "css" > 20 else > 21 echo "no css" > 22 fi > > and received:- > > + '[' law45p07a01=law00css ']' > + echo law45p07a01 > law45p07a01 > + echo css > cssCorrect. You invoked the test command with a single argument, the string "law45p07a01=law00css". With a single argument, the test is just whether that argument in non-null, which it is. To perform a comparison you need 3 separate arguments (2 operands and an operator), not 1. -- Bob Nichols "NOSPAM" is really part of my email address. Do NOT delete it.