我有一个 python 脚本,它从参数中获取输入。我可以将其作为条件放在 bash 脚本的 if 语句中吗?如果 python 脚本以错误结尾(退出 1),则它必须触发另一个 python 脚本,如果 python 脚本以 0 结尾,则无需执行任何操作。我想通过 bash 脚本中的 if 条件来做到这一点。我在代码中出错了。
#!/bin/bash
if [ "python pythonscript.py argument" == 1 ]; then
python pythonscript2.py argument
fi
exit 0
相同的参数被传递给两个 python 脚本。argument 是一个类似 something_Dept-envi 的字符串。当我执行上面没有语法错误同时什么都没有发生时,我什么也没得到
#!/bin/bash
if [ "python pythonscript.py argument" == 1 ]; then
python pythonscript2.py argument
fi
exit 0
如果 python 脚本以错误结束(退出代码 1),我希望使用参数 else exit o 执行另一个 python 代码
停止将 [
视为运算符。这只是一个命令。但是在这种情况下,您不想使用它;你只想调用你的 python 脚本:
if python pythonscript.py argument; then
echo executed when python return 0
else
echo executed when python returns non-zero
fi
对于您的特定情况,您只需要:
if ! python pythonscript.py argument; then
python pythonscript2.py argument
fi
这仅区分零或非零退出,并不检查特定的非零值。如果您想在脚本以 2 退出时与以 1 退出时执行不同的操作,您可以检查 $?
的值,但最好不要这样做。
我是一名优秀的程序员,十分优秀!