trap in shell programming

Bill Davidsen davidsen at tmr.com
Mon Mar 7 18:23:47 UTC 2011


xinyou yan wrote:
> thank you !
>
> As you write . It works .
> I thought it was captured by shell. In fact  the error in cods.
> I am  just a student .
> Someone told me   some skills  are import . However  , I study it and
> find I can't use it right now .
> That lead careless  .
>
> May be we need to find interest in programming .
>
Sounds as if you have mastered:
- ask a decent question
- listen to the answer
- understand what you were told

You have a good start, keep going.


>
>
> 2011/3/7 Cameron Simpson<cs at zip.com.au>:
>> On 07Mar2011 10:17, xinyou yan<yxy.716 at gmail.com>  wrote:
>> | I want to know  when i use
>> |
>> | trap 'rm -f /tmp/my_tmp_file_$$'   INT
>> |
>> | why shell itself  capture the single  and she shellscript exit.
>>
>> But it doesn't!
>>
>> The shell captures the signal, runs the rm command, and proceeds.
>>
>> | Here is the code from<<  Beginning Programming>>
>> |   #!/bin/sh
>> | trap 'rm -f /tmp/my_tmp_file_$$' INT
>> | echo creating file /tmp/my_tmp_file_$$
>> | date>  /tmp/my_tmp_file_$$
>> |
>> | echo "Press interrupt (Ctrl-C) to interrupt...."
>> | while [ -f /tmp/my_tmp_file_$$ ]; do
>> |     echo File exists
>> |     sleep 1
>> | done
>> | echo The file no longer exists
>>
>> When I run this script, this all works just fine.
>>
>> | trap INT
>>
>> However, _this_ line above is incorrect.
>> "trap" requires a shell command to run, even if it is empty.
>> For example:
>>
>>   trap 'echo INT received' INT
>>
>> > From "man sh":
>>
>>   trap [-lp] [[arg] sigspec ...]
>>         The command arg is to  be  read  and  executed  when  the shell
>>         receives  signal(s)  sigspec. [...snip...] If arg is the null
>>         string the signal specified by  each sigspec  is ignored by the shell
>>         and by the commands it invokes. [...snip...]
>>
>> When I run the script it aborts at that line.
>>
>> Try changing that line to:
>>
>>   trap 'echo INT received' INT
>>
>> and rerunning your script.
>>
>> Cheers,
>> --
>> Cameron Simpson<cs at zip.com.au>  DoD#743
>> http://www.cskk.ezoshosting.com/cs/
>>
>> More computing sins have been committed in the name of performance,
>> without necessariliy achieving it, than for all other reasons
>> combined.       - Wulf
>> --
>> users mailing list
>> users at lists.fedoraproject.org
>> To unsubscribe or change subscription options:
>> https://admin.fedoraproject.org/mailman/listinfo/users
>> Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
>>


-- 
Bill Davidsen <davidsen at tmr.com>
   "We have more to fear from the bungling of the incompetent than from
the machinations of the wicked."  - from Slashdot



More information about the users mailing list