发现bash -e参数的一个问题

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
jiandan23
帖子: 86
注册时间: 2010-12-17 22:31
系统: Mint 19.2

发现bash -e参数的一个问题

#1

帖子 jiandan23 » 2020-08-11 13:40

按照道理,加上-e参数,当脚本运行中遇到错误时,会立马退出,但我发现当使用&&时会继续执行。如下所示:
图片
为什么第一行命令的返回值非0,bash还继续往下执行了?
附件
2020-08-11_133554.png
2020-08-11_133554.png (7.69 KiB) 查看 29338 次
头像
astolia
论坛版主
帖子: 6454
注册时间: 2008-09-18 13:11

Re: 发现bash -e参数的一个问题

#2

帖子 astolia » 2020-08-11 16:33

见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参数的一个问题

#3

帖子 jiandan23 » 2020-08-12 9:15

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 ~]#
头像
astolia
论坛版主
帖子: 6454
注册时间: 2008-09-18 13:11

Re: 发现bash -e参数的一个问题

#4

帖子 astolia » 2020-08-12 10:15

你英文水平有待提高。
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参数的一个问题

#5

帖子 jiandan23 » 2020-08-12 10:26

明白了,多谢!
不过bash的这个特性感觉怪怪的。
头像
bzhao
帖子: 250
注册时间: 2008-07-05 2:15
系统: XUbuntu

Re: 发现bash -e参数的一个问题

#6

帖子 bzhao » 2020-08-28 18:31

xxxx && yyy 链接的命令和管道没有关系的了! xxxx | yyy 才是管道连接两个命令!
Bill Z
stat -c %a filename
\_.\{-}
头像
bzhao
帖子: 250
注册时间: 2008-07-05 2:15
系统: XUbuntu

Re: 发现bash -e参数的一个问题

#7

帖子 bzhao » 2020-08-28 18:41

我这样子理解:
aaa && bbb
相当于:
if aaa; then
bbb
fi
aaa 出错不执行bbb, 但是整个 if .... fi 语句是执行成功的 , 这个就是shell的执行流的执行session的栈分层问题了
Bill Z
stat -c %a filename
\_.\{-}
头像
astolia
论坛版主
帖子: 6454
注册时间: 2008-09-18 13:11

Re: 发现bash -e参数的一个问题

#8

帖子 astolia » 2020-08-31 10:46

bzhao 写了: 2020-08-28 18:31 xxxx && yyy 链接的命令和管道没有关系的了! xxxx | yyy 才是管道连接两个命令!
这里有人讨论管道吗?
bzhao 写了: 2020-08-28 18:41 这个就是shell的执行流的执行session的栈分层问题了
你真的明白你在说什么吗?
头像
lilydjwg
论坛版主
帖子: 4250
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 发现bash -e参数的一个问题

#9

帖子 lilydjwg » 2020-11-18 22:15

我这里 bash 在执行完第一句命令之后就退出了。大概是你的 bash 有点问题?
回复