我正在尝试在Tox中运行条件bash命令。
如果用户将“ stag”传递到脚本中,则tox应该运行一个curl命令;否则,tox应该运行一个curl命令。如果他们将“ prod”传递到脚本中,它将运行另一个curl命令。
[testenv]
ENV=$1
whitelist_externals=
/bin/bash
deps=
-rrequirements.txt
commands=
bash -ec 'curl https://this_is_just_sample_test.com'
pytest test/test.py
当我尝试为批处理命令引入条件时:
[testenv]
ENV=$1
whitelist_externals=
/bin/bash
deps=
-rrequirements.txt
commands=
bash -ec 'if [$1 == "stag"]; then curl https://this_is_just_sample_test.com fi'
pytest test/test.py
我收到以下错误消息:
错误:命令'/ bin / bash -ec的InvocationError如果[$ 1 =“ stag”]; 然后卷曲https://this_is_just_sample_test.com fi'(用代码退出 2)
答案 0 :(得分:1)
让我为您修复代码(我为[]
命令添加了空格,将参数更改为$0
并添加了分号):
[testenv]
whitelist_externals=
/bin/bash
deps=
-rrequirements.txt
commands=
bash -ec 'if [ "$0" == "stag" ]; then curl https://this_is_just_sample_test.com; fi' {posargs}
pytest test/test.py
{posargs}
is a substitution,允许从tox
调用中传递命令行参数。像这样:
$ tox stag