linux - 如何在 `set -e` 时有选择地处理非零退出代码

标签 linux bash

我在脚本中设置了

set -e

然后我在所述脚本的 if 语句中运行命令:

if adb -s emulator-5554 uninstall my.package ; then
    echo "handle emulator command return code here ... "
fi 

我想获取命令emulator-5554 uninstall my.package的返回码,并根据其值处理返回码;我无法执行此操作,因为该命令嵌入在 if 语句中。

最佳答案

if 语句中不会影响您获取返回码的方式,并且 set -e 不适用于条件命令:

if adb -s emulator-5554 uninstall my.package ; then
    echo "The command succeeded, yay!"
else
    code="$?"
    echo "The command failed with exit code $code"
fi 

关于linux - 如何在 `set -e` 时有选择地处理非零退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56009935/

相关文章:

bash - bash变量扩展中出现奇怪的字符

linux - 通过终止进程查找应用程序

linux - 如何在 grep 中显示搜索词

linux - 有没有办法定义自定义隐式 GNU Make 规则?

linux - 所有任务的单一公共(public)地址空间

linux - 如何阻止换行符转义旧的 gnu sed 命令

bash - 从Dockerfile和脚本调用时ROS命令不起作用

php - 从 PHP 中使用 sudo 运行 shell 脚本

linux - 使用 acls 设置 linux 文件权限继承

c++ - 通过 recvfrom (UDP) 接收一部分数据包