shell 异常捕获

十一路 2015-06-29 09:56:36
在shell 中执行 .sh或.jar
捕获是否执行错误
trap "" err 这样可以实现

那么我要如何获取错误的内容,然后记录日志
...全文
755 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nswcfd 2015-06-29
  • 打赏
  • 举报
回复
trap my_error_callback ERR,只能捕获简单命令(simple command)。
#!/bin/bash

my_error_callback()
{
        #echo "in_call_back: $*"
        i=0
        while true; do
                echo -n '##############'
                caller $((i++)) || break
        done
        echo
}

trap my_error_callback ERR

my_error_test()
{
        error_here
}

echo 1
error_cmd 2
echo 3
my_error_test
nswcfd 2015-06-29
  • 打赏
  • 举报
回复
If a sigspec is ERR, the command arg is executed whenever a simple command has a non-zero exit status, sub- ject to the following conditions. The ERR trap is not executed if the failed command is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a command executed in a && or ││ list, or if the command’s return value is being inverted via !. These are the same conditions obeyed by the errexit option.

23,117

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧