shell的Trap的一致性問題

來源:互聯網
上載者:User

標籤:shell   trap   

例子1:

[[email protected] example]$ more trapping 

#!/bin/sh

# Scriptname: trapping

# Script to illustrate the trap command and signals

trap ‘echo "Ctrl-C will not terminate $0."‘ 2

trap ‘echo "Ctrl-\ will not terminate $0."‘ 3

echo "Enter any string after the prompt."

echo "When you are ready to exit, type \"stop\"."

while true

do

        echo -n "Go ahead...> "

        read reply

        if [ "$reply" = stop ]

        then

          break

        fi

done

 

 

[[email protected] example]$ sh trapping 

Enter any string after the prompt.

When you are ready to exit, type "stop".

Go ahead...> ^CCtrl-C will not terminate trapping.

註:這表明2訊號是可以捕捉到,並且進行處理

 

Go ahead...> stop

[[email protected] example]$ trap

trap -- ‘‘ SIGTSTP

trap -- ‘‘ SIGTTIN

trap -- ‘‘ SIGTTOU

[[email protected] example]$ 

 

 例子2:

[[email protected] example]$ more trapping 

#!/bin/sh

# Scriptname: trapping

# Script to illustrate the trap command and signals

trap ‘echo "Ctrl-C will not terminate $0."‘ 9

trap ‘echo "Ctrl-\ will not terminate $0."‘ 3

echo "Enter any string after the prompt."

echo "When you are ready to exit, type \"stop\"."

while true

do

        echo -n "Go ahead...> "

        read reply

        if [ "$reply" = stop ]

        then

          break

        fi

done

 

 

[[email protected] example]$ sh trapping 

Enter any string after the prompt.

When you are ready to exit, type "stop".

Go ahead...> Killed

注:這表明9訊號不能捕捉到,更不用說進行處理了

[[email protected] example]$ 

 同步進行如下操作:

[[email protected] ~]$ ps -ef|grep trapping

maokx     5004  2968  0 11:18 pts/0    00:00:00 sh trapping

maokx     5008  3248  0 11:18 pts/1    00:00:00 grep --color=auto trapping

[[email protected] ~]$ kill -9 5004

[[email protected] ~]$ 

總結:這表明trap也無法保證操作的一致性,也證明了shell當中操作不能保證操作的一致性


shell的Trap的一致性問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.