简体   繁体   中英

Getting error in if clause in shell script inside jenkins job

I am having shell script like this

while read -r LINE || [[ -n $LINE ]]; do
noStudent="false"
LINE=$(echo "$LINE" | tr '[:upper:]' '[:lower:]')
if [ $LINE == "myname" ]
then
noStudent="true"
fi

if [ $noStudent == "true" ]
then
  #DO SOME STUFF
fi
done < Teachers

But I am getting error on each line when files is read :

+ [ yourname == myname ]
/tmp/hudson433507028658734743.sh: 20: [: yourname: unexpected operator
+ [ false == true ]
/tmp/hudson433507028658734743.sh: 26: [: false: unexpected operator

What can be the reason for it ? Please help. Am not able to figure out.

Your script isn't actually being executed by bash , but by some other shell linked to by /bin/sh (likely dash , see below). The POSIX standard does not recognize == as a valid operator with the [ command; you need to use = instead.

Using dash , for example, this can be reproduced with

$ [ foo == foo ] && echo same
dash: 5: [: foo: unexpected operator
$ [ foo = foo ] && echo same
same
$

You will almost certainly use [ because you are concerned about portability, in which case you must also use = . If you aren't concerned about portability, and your shell allows == inside [ , you can almost certainly (and probably should) use [[ instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM