gpt4 book ai didi

linux - 如何将另一个脚本的输出编码为其他脚本中的 in 条件

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:40 25 4
gpt4 key购买 nike

我有一个 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 退出时执行不同的操作,您可以检查 $? 的值,但最好不要这样做。

关于linux - 如何将另一个脚本的输出编码为其他脚本中的 in 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55689996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com