小编Jim*_*ker的帖子

防止 SIGINT 中断函数调用和子进程

考虑以下脚本:

#!/bin/bash

set -o pipefail
set -o history

trapper() {
   func="$1" ; shift
   for sig ; do
      trap "$func $sig" "$sig"
   done
}

err_handler () {
  case $2 in
    INT)
       stop_received=1
    ;;
    TSTP)
    ;;
    ERR)
       if [[ $2 != "INT" ]]; then # for some reason, ERR gets triggered on SIGINT
          code=$?
          if [ $code -ne 0 ]; then
             echo "Failed on line $1"
             echo "$BASH_COMMAND returned $?"
             echo "Content of variables at the time of failure:"
             echo "$(set -o …
Run Code Online (Sandbox Code Playgroud)

signals shell-script trap

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

标签 统计

shell-script ×1

signals ×1

trap ×1