在jenkins job中的shell脚本中获取if子句中的错误

时间:2015-12-05 21:32:27

标签: bash shell jenkins sh

我有像这样的shell脚本

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

但是在读取文件时,每行都会出错:

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

它可能是什么原因?请帮忙。我无法弄明白。

1 个答案:

答案 0 :(得分:1)

您的脚本实际上并未由bash执行,而是由/bin/sh链接到的其他一些shell(可能dash,请参阅下文)。 POSIX标准不会将==识别为[命令的有效运算符;您需要改为使用=

例如,使用dash,可以使用

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

您几乎肯定会使用[,因为您担心可移植性,在这种情况下您还必须使用=。如果您不关心可移植性,并且shell允许在==[,则几乎可以肯定(并且可能应该)使用[[