AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 683936
Accepted
Silicomancer
Silicomancer
Asked: 2015-10-11 11:52:31 +0800 CST2015-10-11 11:52:31 +0800 CST 2015-10-11 11:52:31 +0800 CST

在不退出终端的情况下退出错误处理函数中的 shell 脚本

  • 772

我正在写一个 shell 脚本。该 shell 脚本在bash终端内的 shell 中执行。它包含一个中央错误处理函数。请参阅以下基本演示片段:

function error_exit
{
   echo "Error: ${1:-"Unknown Error"}" 1>&2
   exit 1 # This unfortunately also exits the terminal
}

# lots of lines possibly calling error_exit
cd somewhere || error_exit "cd failed"
rm * || error_exit "rm failed"
# even more lines possibly calling error_exit

错误处理函数应该结束脚本但不应该结束终端。我怎样才能做到这一点?

bash
  • 1 1 个回答
  • 3405 Views

1 个回答

  • Voted
  1. Best Answer
    kos
    2015-10-11T12:07:57+08:002015-10-11T12:07:57+08:00

    使用bash的trap内置命令在脚本退出时生成一个bash实例:

    trap 'bash' EXIT
    

    来自help trap:

    trap: trap [-lp] [[arg] signal_spec ...]
        Trap signals and other events.
    
        Defines and activates handlers to be run when the shell receives signals
        or other conditions.
    
        ARG is a command to be read and executed when the shell receives the
        signal(s) SIGNAL_SPEC.  If ARG is absent (and a single SIGNAL_SPEC
        is supplied) or `-', each specified signal is reset to its original
        value.  If ARG is the null string each SIGNAL_SPEC is ignored by the
        shell and by the commands it invokes.
    
        If a SIGNAL_SPEC is EXIT (0) ARG is executed on exit from the shell.
    

    所以通过运行trap 'bash' EXIT,bash将在 shell 接收到信号 EXIT 时读取并执行;产生一个交互式 shell 必然会产生阻止终端关闭的效果:

    function error_exit
    {
       echo "Error: ${1:-"Unknown Error"}" 1>&2
       exit 1 # This unfortunately also exits the terminal
    }
    
    trap 'bash' EXIT
    # lots of lines possibly calling error_exit
    cd somewhere || error_exit "cd failed"
    rm * || error_exit "rm failed"
    # even more lines possibly calling error_exit
    
    • 2

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve