Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell conditional in makefile

Tags:

sh

makefile

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"
like image 541
user1496062 Avatar asked Nov 21 '13 13:11

user1496062


2 Answers

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; \
like image 110
MadScientist Avatar answered Nov 04 '22 22:11

MadScientist


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
like image 4
Manbir Singh Yadav Avatar answered Nov 04 '22 22:11

Manbir Singh Yadav