按照道理,加上-e参数,当脚本运行中遇到错误时,会立马退出,但我发现当使用&&时会继续执行。如下所示:
为什么第一行命令的返回值非0,bash还继续往下执行了?
发现bash -e参数的一个问题
- jiandan23
- 帖子: 86
- 注册时间: 2010-12-17 22:31
- 系统: Mint 19.2
发现bash -e参数的一个问题
- 附件
-
- 2020-08-11_133554.png (7.69 KiB) 查看 29338 次
- astolia
- 论坛版主
- 帖子: 6454
- 注册时间: 2008-09-18 13:11
Re: 发现bash -e参数的一个问题
见manpage里的说明
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a
compound command (see SHELL GRAMMAR above), exits with a non-zero status. The shell does not
exit if the command that fails is part of the command list immediately following a while or un‐
til keyword, part of the test following the if or elif reserved words, part of any command exe‐
cuted in a && or || list except the command following the final && or ||, any command in a
pipeline but the last, or if the command's return value is being inverted with !. If a com‐
pound command other than a subshell returns a non-zero status because a command failed while -e
was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the
shell exits. This option applies to the shell environment and each subshell environment sepa‐
rately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before exe‐
cuting all the commands in the subshell.
- jiandan23
- 帖子: 86
- 注册时间: 2010-12-17 22:31
- 系统: Mint 19.2
Re: 发现bash -e参数的一个问题
Hi,astolia : 之前我也看到了这段话,我对它的理解是,-e对||和&&都无效,但经过测试,||的行为和&&不一样,-e对||有效的,如下:
--------------------------------------------
[root@mytest ~]# cat b.sh
#!/bin/bash
ls asdfaefasfa || ls adfwefasfaf
echo $?
echo here
[root@mytest ~]# bash -e b.sh
ls: 无法访问asdfaefasfa: 没有那个文件或目录
ls: 无法访问adfwefasfaf: 没有那个文件或目录
[root@mytest ~]#
--------------------------------------------
[root@mytest ~]# cat b.sh
#!/bin/bash
ls asdfaefasfa || ls adfwefasfaf
echo $?
echo here
[root@mytest ~]# bash -e b.sh
ls: 无法访问asdfaefasfa: 没有那个文件或目录
ls: 无法访问adfwefasfaf: 没有那个文件或目录
[root@mytest ~]#
- astolia
- 论坛版主
- 帖子: 6454
- 注册时间: 2008-09-18 13:11
Re: 发现bash -e参数的一个问题
你英文水平有待提高。
a && b这种情况是短路求值,在a出错的情况下根本不会执行b,不满足最后的b出错这一条件。而a || b则不然
注意红字部分。只有是最后一个&&或||之后的命令出错才会退出The shell does not exit if the command that fails is part of any command executed in a && or || list except the command following the final && or ||
a && b这种情况是短路求值,在a出错的情况下根本不会执行b,不满足最后的b出错这一条件。而a || b则不然
- jiandan23
- 帖子: 86
- 注册时间: 2010-12-17 22:31
- 系统: Mint 19.2
Re: 发现bash -e参数的一个问题
明白了,多谢!
不过bash的这个特性感觉怪怪的。
不过bash的这个特性感觉怪怪的。
- bzhao
- 帖子: 250
- 注册时间: 2008-07-05 2:15
- 系统: XUbuntu
Re: 发现bash -e参数的一个问题
xxxx && yyy 链接的命令和管道没有关系的了! xxxx | yyy 才是管道连接两个命令!
Bill Z
stat -c %a filename
\_.\{-}
stat -c %a filename
\_.\{-}
- bzhao
- 帖子: 250
- 注册时间: 2008-07-05 2:15
- 系统: XUbuntu
Re: 发现bash -e参数的一个问题
我这样子理解:
aaa && bbb
相当于:
if aaa; then
bbb
fi
aaa 出错不执行bbb, 但是整个 if .... fi 语句是执行成功的 , 这个就是shell的执行流的执行session的栈分层问题了
aaa && bbb
相当于:
if aaa; then
bbb
fi
aaa 出错不执行bbb, 但是整个 if .... fi 语句是执行成功的 , 这个就是shell的执行流的执行session的栈分层问题了
Bill Z
stat -c %a filename
\_.\{-}
stat -c %a filename
\_.\{-}
- astolia
- 论坛版主
- 帖子: 6454
- 注册时间: 2008-09-18 13:11
- lilydjwg
- 论坛版主
- 帖子: 4250
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
- 联系:
Re: 发现bash -e参数的一个问题
我这里 bash 在执行完第一句命令之后就退出了。大概是你的 bash 有点问题?