等待命令失败而没有错误

发布于 2025-01-26 22:41:02 字数 401 浏览 1 评论 0原文

我有以下脚本

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2025-02-02 22:41:02

您可以使用BASH严格模式在代码中启用调试

这是您的代码的更好版本:

#!/bin/bash

# bash strict mode
set -Eeuo pipefail 

# trap for Error signals
trap '1>&2 echo error at line: $LINENO' ERR

ls -l 2>/dev/null > fileone.txt &

df -h  2>/dev/null > filetwo.txt & 

wait

# if there is a difference, exit code with be 1
# if there was no difference, exit code will be 0
diff fileone.txt filetwo.txt

if [ $? != 0 ]; then
    echo "fail"
else
    echo "success"
fi

如果您运行此内容,则结果是:

1,7c1,12
< total 16
< -rw-r--r-- 1 shm shm   0 May  7 23:19 fileone.txt
< -rw-r--r-- 1 shm shm   0 May  7 23:19 filetwo.txt
< -rw-r--r-- 1 shm shm 273 May  7 20:08 index.html
< -rw-r--r-- 1 shm shm 491 May  7 20:07 match_html_id.sh
< -rwxr-xr-x 1 shm shm 251 May  7 23:18 s2.sh
< -rw-r--r-- 1 shm shm 413 May  7 17:40 script.sh
---
> Filesystem         Size  Used Avail Use% Mounted on
> dev                3.8G     0  3.8G   0% /dev
> run                3.9G  1.6M  3.9G   1% /run
> /dev/sda2          228G   24G  193G  11% /
> tmpfs              3.9G  228M  3.6G   6% /dev/shm
> tmpfs              3.9G   95M  3.8G   3% /tmp
> /dev/sda1          300M  288K  300M   1% /boot/efi
> tmpfs              780M   60K  780M   1% /run/user/1000
> /dev/sdb1          916G  289G  582G  34% /mnt
> /dev/mapper/ravan   75M   26M   44M  37% /home/shm/ravan
> /dev/mapper/ssh     75M  4.0M   65M   6% /home/shm/.ssh
> /dev/mapper/pas     75M  224K   69M   1% /home/shm/password
error at line: 13

so 在行:13 这是一行:

13  diff fileone.txt filetwo.txt

正确的结果是正确的,因为两个文件之间存在差异,所以diff的退出代码为1。

如果将命令更改为睡眠1,结果将为成功

You can enable debugging in your code using bash strict mode.

Here is a better version of your code:

#!/bin/bash

# bash strict mode
set -Eeuo pipefail 

# trap for Error signals
trap '1>&2 echo error at line: $LINENO' ERR

ls -l 2>/dev/null > fileone.txt &

df -h  2>/dev/null > filetwo.txt & 

wait

# if there is a difference, exit code with be 1
# if there was no difference, exit code will be 0
diff fileone.txt filetwo.txt

if [ $? != 0 ]; then
    echo "fail"
else
    echo "success"
fi

and if you run this the result is:

1,7c1,12
< total 16
< -rw-r--r-- 1 shm shm   0 May  7 23:19 fileone.txt
< -rw-r--r-- 1 shm shm   0 May  7 23:19 filetwo.txt
< -rw-r--r-- 1 shm shm 273 May  7 20:08 index.html
< -rw-r--r-- 1 shm shm 491 May  7 20:07 match_html_id.sh
< -rwxr-xr-x 1 shm shm 251 May  7 23:18 s2.sh
< -rw-r--r-- 1 shm shm 413 May  7 17:40 script.sh
---
> Filesystem         Size  Used Avail Use% Mounted on
> dev                3.8G     0  3.8G   0% /dev
> run                3.9G  1.6M  3.9G   1% /run
> /dev/sda2          228G   24G  193G  11% /
> tmpfs              3.9G  228M  3.6G   6% /dev/shm
> tmpfs              3.9G   95M  3.8G   3% /tmp
> /dev/sda1          300M  288K  300M   1% /boot/efi
> tmpfs              780M   60K  780M   1% /run/user/1000
> /dev/sdb1          916G  289G  582G  34% /mnt
> /dev/mapper/ravan   75M   26M   44M  37% /home/shm/ravan
> /dev/mapper/ssh     75M  4.0M   65M   6% /home/shm/.ssh
> /dev/mapper/pas     75M  224K   69M   1% /home/shm/password
error at line: 13

So error at line: 13 which is this line:

13  diff fileone.txt filetwo.txt

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 to sleep 1 the result will be success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文