简体   繁体   English

bash - 使用 set -e 时如何处理退出代码并获取命令输出

[英]bash - how to process exit code when using set -e and get output of command

I have set -e at top of script.我在脚本顶部set -e How to avoid script termination for some commands and process exit code?如何避免某些命令的脚本终止和进程退出代码? I have a difficulty in following code sample:我在以下代码示例中遇到困难:
I need both output to variable and exit code, without set -e its easy.我需要输出到变量和退出代码,没有set -e很容易。

set -e
...
formoutput=$(yad --form --field="Subdomain" --field="Domain" --field="Web master username" \
  --field="Apache group" --field='Webroot' \
  --field='Webroot variables - ${homedir}(of webmaster) ${subdomain} ${webmaster} ${domain}:LBL' \
  --field="Virtualhost ip or domain" \
  --field="Virtualhost port" --field="Server admin email" \
  --field="Create mysql user&db:CHK" \
  --button="Cancel:3" --button="Save defaults:2" --button="Create:0" \
  --title="Create apache virtualhost" \
  --text='Subdomain are case sencetive for Webroot folder ${subdomain} variable' \
  --focus-field=1 --center --window-icon="preferences-system" --width=600 \
  "${config[subdomain]}" "${config[domain]}" "${config[webmaster]}" "${config[webgroup]}" \
  "${config[webroot]}" "test" "${config[virtualhost]}" "${config[virtualport]}" "${config[serveradmin]}" 1)
formbutton="$?"

You need to handle the exit code of the command.您需要处理命令的退出代码。

The exit status of the command is the exit status of last command executed.命令的退出状态是最后执行的命令的退出状态。

Variable assignment is not a command.变量赋值不是命令。

So you can just do what's popular in set -e scripts:因此,您可以执行set -e脚本中流行的操作:

formoutput=$(yad ...) || ret=$? && ret=$?

to catch the return value.捕获返回值。

Or you can ignore the exit by invoking : command:或者您可以通过调用:命令忽略退出:

formoutput=$(yad ...) ||:

The ||: is really || ||:真的是|| with : command.:命令。 The colon command returns exit status of zero, the exit status of the list of commands a || b冒号命令返回退出状态为零,命令列表的退出状态a || b a || b is the exit status of the last command executed in the list, as : always returns zero, the list of commands will also return zero exit status. a || b是列表中执行的最后一个命令的退出状态,因为:总是返回零,命令列表也将返回零退出状态。

Or use if :或者使用if

if ! formoutput=$(yad ...); then 
          echo "AAAA! yad failed! abort ship!" >&2
fi

Note that if command also has an exit status of the last command executed.请注意, if命令还具有最后执行的命令的退出状态。 So be aware that:所以请注意:

if true; then
    false
fi

will exit from your set -e script.将从您的set -e脚本中退出。

Try this:尝试这个:

set -e
false || echo $? && echo 0

Even though false exits with 1, you'll be able to capture this exit value in the middle (with $? ) without aborting the whole script.即使false以 1 退出,您也可以在中间(使用$? )捕获此退出值,而无需中止整个脚本。

If the script ( false ) didn't fail, the exit code is always zero so you can just put a zero on success.如果脚本 ( false ) 没有失败,则退出代码始终为零,因此您可以将成功设为零。

I needed to use the operators in the other order for this to work我需要以其他顺序使用运算符才能使其工作

$ set -e
$ true || status=$? && status=$?; echo $status
0
$ false || status=$? && status=$?; echo $status
0
$ true && status=$? || status=$?; echo $status
0
$ false && status=$? || status=$?; echo $status
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM