linux脚本 trap,linux脚本----trap

#!/bin/bash

# Hunting variables with a trap.

trap 'echo Variable Listing --- a = $a b = $b' EXIT

# EXIT is the name of the signal generated upon exit from a script.

#

# The command specified by the "trap" doesn't execute until

#+ the appropriate signal is sent.

echo "This prints before the \"trap\" --"

echo "even though the script sees the \"trap\" first."

echo

a=39

b=36

exit 0

# Note that commenting out the 'exit' command makes no difference,

#+ since the script exits in any case after running out of commands.[root@localhost shell]# ./ex76.sh

This prints before the "trap" --

even though the script sees the "trap" first.

Variable Listing --- a = 39 b = 36

格式:trap "commands" signals

当shell接收到signals指定的信号时,执行commands命令。(The  command arg is to be read and executed when the shell receives signal(s) sigspec. )

格式:trap signals

如果没有指定命令部分,那么就将信号处理复原。比如 trap INT 就表明恢复Ctrl+C退出。(If arg is absent (and there is a single sigspec) or -, each specified signal is reset to its  original  disposition  (the value  it  had  upon  entrance  to  the  shell). )

格式:trap "" signals

忽略信号signals,可以多个,比如 trap "" INT 表明忽略SIGINT信号,按Ctrl+C也不能使脚本退出。又如 trap "" HUP 表明忽略SIGHUP信号,即网络断开时也不能使脚本退出。(If arg is the null string the signal specified by each sigspec is ignored by the shell and by the commands it invokes. )

格式:trap -p

格式:trap -p signal

把当前的trap设置打印出来。(If arg is not present and -p  has  been supplied,  then  the trap commands associated with each sigspec are displayed.  If no arguments are supplied or if only -p is given, trap prints the list of commands associated  with  each  signal.)

格式:trap -l

把所有信号打印出来。(The  -l option  causes  the shell to print a list of signal names and their corresponding numbers.  Each sigspec is either a signal name defined in , or a signal number.  Signal names  are  case  insensitive and  the  SIG prefix is optional.)

格式:trap "commands" EXIT

脚本退出时执行commands指定的命令。(If a sigspec is EXIT (0) the command arg is executed on exit from the shell.)

格式:trap "commands" DEBUG

在脚本执行时打印调试信息,比如打印将要执行的命令及参数列表。(If a sigspec is DEBUG, the command arg is executed before every  simple  command,  for  command, case  command,  select command, every arithmetic for command, and before the first command executes in a shell function (see SHELL GRAMMAR above).  Refer to the description of the extdebug option to the  shopt builtin  for  details of its effect on the DEBUG trap.)

格式:trap "commands" ERR

当命令出错,退出码非0,执行commands指定的命令。(If a sigspec is ERR, the command arg is executed whenever a simple command has a non-zero exit status, subject 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 && or ┅Ι│ list, or if the command’s return  value is  being  inverted via !.  These are the same conditions obeyed by the errexit option.)

格式:trap "commands" RETURN

当从shell函数返回、或者使用source命令执行另一个脚本文件时,执行commands指定的命令。(If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the . or source    builtins  finishes  executing.   Signals  ignored  upon  entry  to the shell cannot be trapped or reset. Trapped signals that are not being ignored are reset to their original values in a child process when it is created.  The return status is false if any sigspec is invalid; otherwise trap returns true.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux trap是一种命令,用于在shell脚本中设置陷阱,以便在发生特定事件时执行某些操作。它可以用于捕获和处理信号,以及在脚本执行期间处理错误和异常情况。使用trap命令可以提高脚本的可靠性和稳定性。 ### 回答2: Linux trap 指的是在 Linux 操作系统中使用 trap 命令来捕获和处理信号的机制。在 Linux 中,信号是一种进程间通信的方式,用于通知进程发生的事件,如键盘输入、程序异常等。 trap 命令可以用来为进程注册一个信号处理函数。当进程接收到指定的信号时,会执行注册的处理函数来处理这个信号。trap 命令的语法如下: trap [命令] [信号] 其中,命令表示要执行的处理函数或命令,信号表示要捕获处理的信号。 通过使用 trap 命令,我们可以实现对不同信号的处理和控制。例如,我们可以使用 trap 命令来设置一个处理函数,当进程接收到中断信号(SIGINT)时,执行清理操作,并退出程序。 另外,trap 命令还可以用来屏蔽或忽略某些信号,或者在某个特定的时刻捕获和处理信号等。这种灵活的信号处理机制能够帮助我们实现更加健壮和可靠的程序。 总之,Linux 中的 trap 命令是一种用于捕获和处理信号的机制。通过使用 trap 命令,我们可以为进程注册信号处理函数,以实现对不同信号的响应和处理,从而提高程序的稳定性和可靠性。 ### 回答3: Linux trap 是用于捕捉信号的命令,也可以称之为信号陷阱。在Linux中,进程可以通过发送信号与其他进程进行通信或者进行进程管理。而trap命令则可以用来定义一个函数或者命令,在收到指定信号时执行特定的操作。 使用trap命令可以在Shell脚本中捕获指定的信号,比如SIGINT(中断信号)或者SIGTERM(终止信号),从而在收到这些信号时进行相应的处理。可以使用trap命令为指定信号设置处理函数或者指定命令,也可以使用trap命令屏蔽或者忽略指定的信号。 一个经典的用法是在脚本中使用trap命令来捕获SIGINT信号(通过按下Ctrl+C产生),以实现在脚本执行过程中可以通过按下Ctrl+C终止脚本执行的功能。另外,通过trap命令,还可以实现在收到指定信号之前或之后执行一些清理工作的功能。 例如,可以使用trap命令来定义一个函数,用于在脚本执行被中断时打印提示信息并执行清理工作,如关闭打开的文件等。然后使用trap命令将这个函数关联到SIGINT信号。这样,当脚本收到SIGINT信号时,会自动执行这个函数,实现了中断处理的逻辑。 总而言之,Linux trap 是一种用于捕捉信号的命令,可以在Shell脚本中通过设置处理函数或者指定命令,实现对收到指定信号时的处理。它为Shell脚本提供了更加灵活的信号处理方式,可以增强脚本的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值