trap command

本文详细介绍了如何在Shell脚本中使用trap命令捕获并处理中断信号(如Ctrl+C),确保在用户中断脚本执行时能够清理临时文件并保持脚本的正常运行流程。通过修改脚本以实现信号处理,用户可以避免意外终止脚本导致的数据丢失问题。

Consider following script example:

$ cat > testsign
ls -R /

Save and run it as
$ chmod +x testsign
$ ./testsign

Now if you press ctrl + c , while running this script, script get terminated. The ctrl + c(ctrl + c: stop; ctrl + d: save) here work as signal, When such signal occurs its send to all process currently running in your system. Now consider following shell script:

$ cat > testsign1
#
# Why to trap signal, version 1
#
Take_input1()
{
 recno=0
 clear
 echo "Appointment Note keeper Application for Linux"
 echo -n "Enter your database file name : "
 read filename
if [ ! -f $filename ]; then
  echo "Sorry, $filename does not exit, Creating $filename database"
  echo "Appointment Note keeper Application database file" > $filename
fi
echo "Data entry start data: `date`" >/tmp/input0.$$
#
# Set a infinite loop
#
while :
do
     echo -n "Appointment Title:"
     read na
     echo -n "time :"
     read ti
     echo -n "Any Remark :"
     read remark
     echo -n "Is data okay (y/n) ?"
     read ans
if [ $ans = y -o $ans = Y ]; then
    recno=`expr $recno + 1`
    echo "$recno. $na $ti $remark" >> /tmp/input0.$$
fi
echo -n "Add next appointment (y/n)?"
read isnext
 if [ $isnext = n -o $isnext = N ]; then
     cat /tmp/input0.$$ >> $filename
     rm -f /tmp/input0.$$
    return # terminate loop
 fi
done
}
#
#
# Call our user define function : Take_input1
#
Take_input1

Save it and run as
$ chmod +x testsign1
$ ./testsign1

It first ask you main database file where all appointment of the day is stored, if no such database file found, file is created, after that it open one temporary file in /tmp directory, and puts today's date in that file. Then one infinite loop begins, which ask appointment title, time and remark, if this information is correct its written to temporary file, After that, script asks user , whether he/she wants to add next appointment record, if yes then next record is added , otherwise all records are copied from temporary file to database file and then loop will be terminated. You can view your database file by using cat command. Now problem is that while running this script, if you press CTRL + C, your shell script gets terminated and temporary file are left in /tmp directory. For e.g. try it as follows
$./testsign1
After given database file name and after adding at least one appointment record to temporary file press CTRL+C, Our script get terminated, and it left temporary file in /tmp directory, you can check this by giving command as follows
$ ls /tmp/input*
Our script needs to detect such signal (event) when occurs; To achieve this we have to first detect Signal using trap command.
Syntax: 
trap {commands} {signal number list}

Signal NumberWhen occurs
0shell exit 
1hangup
2interrupt (CTRL+C)
3quit 
9kill (cannot be caught)

To catch signal in above script, put trap statement before calling Take_input1 function as trap del_file 2 ., Here trap command called del_file() when 2 number interrupt ( i.e. CTRL+C ) occurs. Open above script in editor and modify it so that at the end it will look like as follows:

$ vi testsign1
#
# signal is trapped to delete temporary file , version 2
#
del_file()
{
  echo "* * * CTRL + C Trap Occurs (removing temporary file)* * *"
  rm -f /tmp/input0.$$
  exit 1
}


Take_input1()
{
recno=0
clear
echo "Appointment Note keeper Application for Linux"
echo -n "Enter your database file name : "
read filename
if [ ! -f $filename ]; then
  echo "Sorry, $filename does not exit, Creating $filename database"
  echo "Appointment Note keeper Application database file" > $filename
fi
echo "Data entry start data: `date`" >/tmp/input0.$$
#
# Set a infinite loop
#
while :
do
  echo -n "Appointment Title:"
  read na
  echo -n "time :"
  read ti
  echo -n "Any Remark :"
  read remark
  echo -n "Is data okay (y/n) ?"
  read ans
  if [ $ans = y -o $ans = Y ]; then
   recno=`expr $recno + 1`
   echo "$recno. $na $ti $remark" >> /tmp/input0.$$
  fi
  echo -n "Add next appointment (y/n)?"
  read isnext
  if [ $isnext = n -o $isnext = N ]; then
    cat /tmp/input0.$$ >> $filename
    rm -f /tmp/input0.$$
    return # terminate loop
  fi
done # end_while
}
#
# Set trap to for CTRL+C interrupt i.e. Install our error handler
# When occurs it first it calls del_file() and then exit
#
trap del_file 2
#
# Call our user define function : Take_input1
#
Take_input1

Run the script as:
$ ./testsign1

After giving database file name and after giving appointment title press CTRL+C, Here we have already captured this CTRL + C signal (interrupt), so first our function del_file() is called, in which it gives message as "* * * CTRL + C Trap Occurs (removing temporary file)* * * " and then it remove our temporary file and then exit with exit status 1. Now check /tmp directory as follows
$ ls /tmp/input*
Now Shell will report no such temporary file exit.


Packets to the CPU The device offers tight control of specific types of traffic to be sent to the CPU. In the device architecture, there is no implicit flooding of Unknown Unicast, Multicast, and Broadcast traffic to the CPU port. In addition, Egress Filtering mechanisms (VLAN egress filtering and Spanning Tree state egress filtering) can be applied to packets sent to the CPU port, as well as to any other network egress port. A packet is sent to the CPU as a result of one of the following actions:  Packet is assigned a TRAP command by an ingress processing engine (Section 22.1.5, TRAP Command).  Packet is assigned a MIRROR command by an ingress processing engine (Section 22.1.4, MIRROR Command).  Packet is assigned a FORWARD command to the virtual CPU ePort 63 by an ingress processing engine (Section 22.1.3, FORWARD Command).  Packet is selected for sampling to the CPU by the ingress or egress ePort sampling mechanism (Section 32.3, Traffic Sampling to the CPU).  Packet is mirrored to ingress/egress analyzer ePort and the designated analyzer ePort is the virtual CPU ePort. In cascaded topologies, it may not be desirable for packets received on a physical cascade port to be mirrored to the CPU, as this may result in the packet being mirrored to the CPU on each device that it traverses in the cascaded system (refer to Section 11.15.2, Multicast Routing in a Cascaded System). To support such configurations, each physical port can be configured to disable mirroring a packet to the CPU. Configuration  To control the mirroring of packets to the CPU based on physical source port, configure <Port<r*32+27>Duplicate ToCPUEn>– <Port<r*32+0>Duplicate ToCPUEn> field in the Duplication of Packets to CPU Configuration r Register (r=0–3) (Table 801 p. 2732) accordingly.  To control the mirroring of packets to the CPU based on source ePort, configure the <Duplicate To CPU En 1> and the <Duplicate To CPU En 2> field in the EQ Ingress ePort Table (Table 822 p. 2743) accordingly. 23.2.1 CPU Code Assignment Packets sent to the CPU are assigned an 8-bit CPU Code, which indicates the reason the packet is being sent to the CPU. In addition, the CPU Code determines the attributes controlling how the packet is sent to the CPU, according to the CPU Code table (Section 23.2.5, CPU Code Table). Many mechanisms throughout the ingress pipeline can trigger a packet to be sent to the CPU. Most of these mechanisms assign a fixed CPU Code, indicating the mechanism or the event that triggered sending the packet to the CPU. The list of CPU Codes is found in Appendix B, CPU and Drop Codes. In addition to the fixed assigned values, there is also a range of user-defined CPU Codes. For further details, refer to Appendix B, CPU and Drop Codes. When multiple engines assign a packet command MIRROR or TRAP, the packet command and CPU Code is resolved according to the resolution table in Section 22.3, Command Resolution Matrix. Various mechanisms allow the application to assign the CPU Code for the trapped or mirrored packet. Examples of these mechanisms are listed below:  If the Ingress Policy engine Action entry command is TRAP or MIRROR, the Action entry contains the specific user-defined CPU Code to be assigned to the packet (refer to Section 9.9.1, IPCL Action Table).If the Tunnel-Termination-Interface (TTI) engine action entry command is TRAP or MIRROR, the Action entry contains the specific user-defined CPU Code to be assigned to the packet (refer to Section 6.9, TTI Action Entry).  Pre-Egress Application Specific CPU Code assignment (Section 23.2.4, Application-Specific CPU Codes) 翻译并解释
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值