标签: bash-trap

eash中的Bash,CTRL + C不会中断主脚本

在我的bash脚本中,我正在运行一个存储在$cmd变量中的外部命令.(它可能是任何东西,甚至是一些简单的bash oneliner.)

如果在运行脚本时按下ctrl+ C,我希望它杀死当前正在运行$cmd但仍应继续运行主脚本.但是,我想保留在主脚本运行时使用ctrl+ 杀死主脚本的选项C.

#!/bin/bash
cmd='read -p "Ooook?" something; echo $something; sleep 4 ' 
while true; do
    echo "running cmd.."
    eval "$cmd"     # ctrl-C now should terminate the eval and print "done cmd"
    echo "done cmd"
    sleep 5         # ctrl-C now should terminate the main script
done
Run Code Online (Sandbox Code Playgroud)

任何想法如何做一些不错的bash方式?

根据答案应用的更改:

#! /bin/bash

cmd='read -p "Ooook1?" something; read -p "Oook2?" ; echo $something; sleep 4 ' 
while true; do
    echo …
Run Code Online (Sandbox Code Playgroud)

bash signals bash-trap

5
推荐指数
1
解决办法
4604
查看次数

如何在全局中捕获bash中的on_error?

似乎on_errorBash中的陷阱仅在其定义的函数范围内起作用.例如,运行此脚本

#!/bin/bash

on_error() {
    echo 'on_error'
}

f() {
    false
    echo 'function f'
}

g() {
    trap on_error ERR
    echo 'function g'
    false
    f
}

g
Run Code Online (Sandbox Code Playgroud)

生产:

function g
on_error
function f
Run Code Online (Sandbox Code Playgroud)

有没有办法on_error全局陷阱,这样我就不必分别将它捕获到每个函数中?

linux error-handling bash shell bash-trap

4
推荐指数
1
解决办法
632
查看次数

如何在Shell脚本中捕获exit 1信号?

我想尝试捕获信号 1 但失败

#!/bin/bash
# capture an interrupt # 0
trap 'echo "Exit 0 signal detected..."' 0
trap 'echo "Exit 1 signal detected..."' SIGHUP

# display something
echo "This is a checkpoint 1"
exit 1

echo "This is checkpoint 2"
# exit shell script with 0 signal
exit 0

Output--
kithokit@15:02:55 trunk (master) $ ./test.sh 
This is a checkpoint 1
Exit 0 signal detected...
kithokit@15:03:44 trunk (master) $ 
Run Code Online (Sandbox Code Playgroud)

即使是退出1,它总是陷入陷阱0,有人知道如何解决这个问题吗?

谢谢

shell signals exit-code bash-trap

4
推荐指数
1
解决办法
2万
查看次数

Bash 陷阱、捕获并将它们作为同一函数的参数传递

我正在开发一个管理一些陷阱的脚本。一开始我只用这段代码管理 INT 和 SIGTSTP,它运行得很好:

#!/bin/bash
function capture_traps() {
    echo -e "\nDoing something on exit"
    exit 1
}

trap capture_traps INT
trap capture_traps SIGTSTP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
Run Code Online (Sandbox Code Playgroud)

然后我尝试添加我想要管理的新陷阱,即 SIGINT 和 SIGHUP。首先,我这样做了(这是有效的):

#!/bin/bash
function capture_traps() {
    echo -e "\nDoing something on exit"
    exit 1
}

trap capture_traps INT
trap capture_traps SIGTSTP
trap capture_traps SIGINT
trap capture_traps SIGHUP
read -p "Script do …
Run Code Online (Sandbox Code Playgroud)

bash bash-trap

4
推荐指数
1
解决办法
1299
查看次数

如何在 if 语句中捕获错误

运行以下代码:

#!/bin/bash

set -o pipefail
set -o errtrace
set -o nounset
set -o errexit

function err_handler ()
{
    local error_code="$?"
    echo "TRAP!"
    echo "error code: $error_code"
    exit
}
trap err_handler ERR

echo "wrong command in if statement"

if xsxsxsxs 
    then
        echo "if result is true"
    else
        echo "if result is false"
fi

echo -e "\nwrong command directly"
xsxsxsxs 

exit
Run Code Online (Sandbox Code Playgroud)


产生以下输出:

wrong command in if statement
trap.sh: line 21: xsxsxsxs: command not found
if result is false

wrong command directly
trap.sh: …
Run Code Online (Sandbox Code Playgroud)

error-handling bash if-statement bash-trap

3
推荐指数
1
解决办法
2975
查看次数

ERR 背后的实际信号是什么

我在几个地方(包括 SO)读过-e被认为是“糟糕的形式”并且在出现任何错误时退出脚本是不可靠的。处理错误的更好方法似乎是使用trap,例如:

trap "echo there was an error; exit 1;" ERR
Run Code Online (Sandbox Code Playgroud)

我似乎无法在手册页中找到ERR实际信号是什么?我假设它是,SIGQUIT但我无法确定。

man 7 signal
Run Code Online (Sandbox Code Playgroud)

只有您期望的正常信号SIGTERM SIGQUIT SIGINT等。

man trap
Run Code Online (Sandbox Code Playgroud)

有对ERR信号的引用,但似乎没有定义它。

例如:“ A trap on ERR, if set, is executed before the shell exits.

man bash
Run Code Online (Sandbox Code Playgroud)

类似于man trapin that is 引用ERR但没有根据我所看到的来定义它。

捷径背后的实际信号是什么ERR?(在正常信号中,如 中所示man 7 signal)。

我更喜欢捕获实际的信号名称而不是速记版本,尽管我意识到它们会产生相同的结果(从脚本中的命令捕获任何错误,然后抛出到处理程序)。

linux error-handling bash signals bash-trap

3
推荐指数
1
解决办法
1942
查看次数

进程退出时未传递 SIGCHLD

我需要检测我的后台进程之一何时退出。因此,我安装了一个陷阱。run_gui并且run_ai1是简单的exec函数。

run_gui & gui_pid=$!
run_ai1 & ai1_pid=$!
trap 'echo foo' SIGCHLD
while true; do
    echo "Started the loop"
    while true; do
        read -u $ai1_outfd line || echo "Nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
    while true; do
        read -u $gui_outfd line || echo "nothing read"
        if [[ $line ]]; then
            : # Handle this
        fi
    done
done
Run Code Online (Sandbox Code Playgroud)

当我关闭 GUI 时,没有任何反应。该echo foo命令仅在我按 ctrl+c 时执行。

为什么我想念SIGCHLD

bash bash-trap

3
推荐指数
1
解决办法
749
查看次数

bash 陷阱 EXIT $LINENO 始终为 1

测试文件

#!/bin/bash
set -e
trap 'echo $LINENO' EXIT
/bin/false
Run Code Online (Sandbox Code Playgroud)

跑步

$ ./test.sh
1
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到“/bin/false”的实际行号而不是总是“1”?

bash shell bash-trap

3
推荐指数
1
解决办法
1028
查看次数

为什么 bash 中的 trap 在分配空变量时会抛出错误

我已经使用 trap 函数有一段时间了,但遇到了一个我不明白的问题。

这是一个代表:

err_report() {
    
    echo -e "ERROR LINE $1"
    exit 1;
}
trap 'err_report ${LINENO}' ERR

existing=$(echo test | grep -oP "x")
Run Code Online (Sandbox Code Playgroud)

这将引发错误,因为 grep 的结果为空。当我在没有陷阱的情况下运行代码时,一切正常。我尝试设置set +u但没有帮助..

我在这里做错了什么?

谢谢

bash is-empty bash-trap

3
推荐指数
1
解决办法
273
查看次数

如何仅在 bash 中捕获 exit 1

我有以下 bash 脚本:

#!/bin/bash


function hello {
    echo "Hello World!"
}

trap hello EXIT
Run Code Online (Sandbox Code Playgroud)

问题是该函数将在该脚本的任何退出代码上执行。如果我exit 1在脚本末尾附加 an ,Hello World!将会被打印,但它也会在 exit 0 上打印。

有没有办法在脚本中运行函数,但前提是脚本仅以退出代码 1 退出?(实际上除了 0 之外的任何值都可以。)

linux bash bash-trap

3
推荐指数
1
解决办法
2098
查看次数