意外的运算符 - Bamboo 计划中的 shell 脚本
Unexpected operator - shell script in a Bamboo plan
我尝试在我的 Bamboo 计划中使用 运行 shell 命令。
这是我尝试 运行:
的代码
[ "$(git rev-parse --abbrev-ref HEAD)" == *test* ] || [ "$(git rev-parse --abbrev-ref HEAD)" == *develop* ] && echo "yes"
应该检查分支名称是 test
或 develop
。如果是,那么它应该打印消息 yes
.
我运行这个在分支develop
上,然后我得到了错误。
错误信息:
[: develop: unexpected operator
更新:
这是 POSIX 所以基于 post:
String comparison in bash. [[: not found
我用一个 =
.
替换了 ==
所以我的命令看起来是:
[ "$(git rev-parse --abbrev-ref HEAD)" = *test* ] || [ "$(git rev-parse --abbrev-ref HEAD)" = *develop* ] && echo "yes"
我收到的错误消息:
[: pytest.ini: unexpected operator
不知道 pytest.ini 在这里做什么。
我的应用程序使用 pytest 但在这一步我没有 运行 它。
单括号的问题是通配符是通过文件名扩展(通配符)扩展的。考虑一下:
$ touch test{1,2,3,4}
$ set -x
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ] || [ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ] && echo "yes"
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]
++ git rev-parse --abbrev-ref HEAD
+ '[' develop == test1 test2 test3 test4 ']'
-bash: [: too many arguments
如果您的 shell 中有双括号可用,那么您可以将单括号替换为双括号:
[[ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]] \
|| [[ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ]] && echo "yes"
作为替代方案,您可以使用 case
,如下所示:
case "$(git rev-parse --abbrev-ref HEAD)" in
*test*|*develop*)
echo 'yes'
;;
esac
注意,单左括号 ([
) 只是 test
内置函数的同义词,唯一的例外是单左括号要求最后一个参数为 ]
。
我尝试在我的 Bamboo 计划中使用 运行 shell 命令。 这是我尝试 运行:
的代码[ "$(git rev-parse --abbrev-ref HEAD)" == *test* ] || [ "$(git rev-parse --abbrev-ref HEAD)" == *develop* ] && echo "yes"
应该检查分支名称是 test
或 develop
。如果是,那么它应该打印消息 yes
.
我运行这个在分支develop
上,然后我得到了错误。
错误信息:
[: develop: unexpected operator
更新:
这是 POSIX 所以基于 post:
String comparison in bash. [[: not found
我用一个 =
.
==
所以我的命令看起来是:
[ "$(git rev-parse --abbrev-ref HEAD)" = *test* ] || [ "$(git rev-parse --abbrev-ref HEAD)" = *develop* ] && echo "yes"
我收到的错误消息:
[: pytest.ini: unexpected operator
不知道 pytest.ini 在这里做什么。 我的应用程序使用 pytest 但在这一步我没有 运行 它。
单括号的问题是通配符是通过文件名扩展(通配符)扩展的。考虑一下:
$ touch test{1,2,3,4}
$ set -x
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ] || [ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ] && echo "yes"
$ [ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]
++ git rev-parse --abbrev-ref HEAD
+ '[' develop == test1 test2 test3 test4 ']'
-bash: [: too many arguments
如果您的 shell 中有双括号可用,那么您可以将单括号替换为双括号:
[[ "$(git rev-parse --abbrev-ref HEAD)" == *"test"* ]] \
|| [[ "$(git rev-parse --abbrev-ref HEAD)" == *"develop"* ]] && echo "yes"
作为替代方案,您可以使用 case
,如下所示:
case "$(git rev-parse --abbrev-ref HEAD)" in
*test*|*develop*)
echo 'yes'
;;
esac
注意,单左括号 ([
) 只是 test
内置函数的同义词,唯一的例外是单左括号要求最后一个参数为 ]
。