13

Cant get this to work , tried everything for the conditional [] bash brackets quotes etc

The if $$? != "0" never matches but the $$? is non zero at times. Any ideas ?

test: testdrivers
-@rc=0; \
   for file in $(TSTFILES); do  \
     ./$$file; \
     if  $$? != "0" ; then  \
     echo test fail;\
                 rc=`expr $$rc + 1` ;\
     fi \
    done; \
  echo; echo "Tests failed: $$rc"

2 Answers 2

19

The right syntax for a numeric comparison in shell scripting is:

if [ $$? -ne 0 ]; then

Be sure you have spaces BEFORE and AFTER the square brackets. They cannot be in the same word as the arguments.

Also you're missing a semicolon after the fi:

    fi; \
done; \
6
  • 1
    I'd probably say something like "on both sides of the opening square bracket and before the closing one" instead since ];, as you used, is perfectly fine. Though I'll grant that is certainly less pithy. Commented Nov 21, 2013 at 15:27
  • Wouldn't be bad to say that [ is actually the same external program as test (although it is likely to be builtin in large shells). So, man test.
    – keltar
    Commented Nov 21, 2013 at 17:49
  • Then there's something else wrong. The syntax I provided, based on your example, will definitely work. What does "doesn't appear to be working" mean? What command do you type? What errors do you see? Please cut and paste directly, rather than retyping, as retyping often introduces subtle changes that make the problem unresolvable by us. Edit your question and cut and paste your makefile rule as you currently have it. Commented Nov 21, 2013 at 23:21
  • It just doesnt match the conditional , i get no errors but i can echo $rc and see it is non zero.. Maybe because its not bash and i should just call a script. Commented Nov 22, 2013 at 1:08
  • Pulled it out into a seperate script and it worked ( which is better anyway) Commented Nov 22, 2013 at 3:45
4

Use below one as an example:

SHELL=/bin/bash
abc=all 
all:
    @ echo $(SHELL)
    @if [ $@ == $(abc) ]; then echo hi; fi;
    @echo done

output will be:

/bin/bash
hi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.