等待命令失败而没有错误
我有以下脚本
command1 2>/dev/null > fileone.txt &
command2 2>/dev/null > filetwo.txt &
wait
diff fileone.txt filetwo.txt
if [ $? != 0 ]; then
echo "fail"
else
echo "success"
fi
,在调用./ myScript.sh
时。该命令在我的控制台上没有错误。
但是echo $?
返回1
我认为这是一个超时问题。有没有办法验证这一点? (或在调用等待
时明确设置超时
I have the following script
command1 2>/dev/null > fileone.txt &
command2 2>/dev/null > filetwo.txt &
wait
diff fileone.txt filetwo.txt
if [ $? != 0 ]; then
echo "fail"
else
echo "success"
fi
and when calling it ./myscript.sh
. the command fails with no error on my console.
However echo $?
returns 1
I am assuming it is a timeout issue. Is there a way to validate this? (or to set a timeout explicitly when invoking wait
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用BASH严格模式在代码中启用调试。
这是您的代码的更好版本:
如果您运行此内容,则结果是:
so 在行:13 这是一行:
正确的结果是正确的,因为两个文件之间存在差异,所以
diff
的退出代码为1。如果将
命令
更改为睡眠1
,结果将为成功。You can enable debugging in your code using bash strict mode.
Here is a better version of your code:
and if you run this the result is:
So error at line: 13 which is this line:
And the result is right since there is a difference between two files so exit code for
diff
is 1.If you change your
commands
tosleep 1
the result will be success.