0

私はこれを使用しています:

$ uname -a
CYGWIN_NT-6.1 bassoon 1.7.15(0.260/5/3) 2012-05-09 10:25 i686 Cygwin
$ bash --version
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
$ cat myexpr.sh
#!/bin/sh

echo "In myexpr, Before  expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After  expr"

$ cat myexpr2.sh
#!/bin/sh

set -e

echo "In myexpr, Before  expr"
ac_optarg=`expr x--with-gnu-as : 'x[^=]*=\(.*\)'`
echo "ac_optarg=$ac_optarg"
echo "In myexpr, After  expr"

2 つのスクリプトの唯一の違いは、myexpr2.sh が「set -e」を使用することです。

$ echo $$
2880
$ ./myexpr.sh
In myexpr, Before  expr
ac_optarg=
In myexpr, After  expr
$ ./myexpr2.sh
In myexpr, Before  expr

これまでのところ、予想される動作。

親シェルでこれを行う場合 (上記の PID 2880):

$ set -e
$ ./myexpr.sh

親シェルが終了します! それは、「set -e」を実行した上記の pID 2880 です。

これは、Linux または cygwin 1.5.12 での動作ではありません。これは cygwin または cygwin の BASH のバグですか?

4

1 に答える 1

0

これはバグではなく、Bash 環境の機能です。これは、Bash シェル環境変数execfailが設定されていない場合や、シェル環境変数errexitが設定されていない場合に発生します。

execfail - (is a BASHOPTS)

If set, a non-interactive shell will not exit if it cannot execute 
the file specified as an argument to the exec builtin command. 
An interactive shell does not exit if exec fails.

errexit -  (is a SHELLOPTS)

Exit immediately if a pipeline (see Pipelines), which may consist of a 
single simple command (see Simple Commands), a subshell command enclosed 
in parentheses (see Command Grouping), or one of the commands executed as 
part of a command list enclosed by braces (see Command Grouping) returns 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 until keyword, part 
of the test in an if statement, part of any command executed 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 status is being inverted 
with !. A trap on ERR, if set, is executed before the shell exits.

This option applies to the shell environment and each subshell environment 
separately (see Command Execution Environment), and may cause subshells to 
exit before executing all the commands in the subshell.

Linux のバージョンが異なれば、これらのデフォルトも異なります。有効になっているものを次の方法で確認できます。

echo "SHELLOPTS=$SHELLOPTS"
echo "BASHOPTS=$BASHOPTS"

そして、次を使用してそれらすべてを表示できます。

set -o && echo -e "\n" && shopt -p 

したがって、次の方法で有効にする必要があります。

shopt -s execfail

それでもうまくいかない場合は、次のようにして $SHELLOPTSのerrexitを設定解除 (オフ) する必要がある場合もあります。

set -o errexit

詳細については、GNU Bash マニュアルを参照してください。

PS。「set」は逆のロジックを使用しているため、「e」フラグを使用する場合は「+」を使用する必要があります。set +e

于 2013-01-16T04:29:13.450 に答える