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.
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
trap 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 We never get here
exit 0
On 07Mar2011 10:17, xinyou yan yxy.716@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,
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 .
2011/3/7 Cameron Simpson cs@zip.com.au:
On 07Mar2011 10:17, xinyou yan yxy.716@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@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@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
On 07Mar2011 16:01, xinyou yan yxy.716@gmail.com wrote: | thank you ! | | As you write . It works . | I thought it was captured by shell. In fact the error in cods.
Tiny errors like this can be hard to see. You can find running scripts like this:
sh -x your_script.sh ...
will show you what commands are actually being executed. They you can see what is and is not happening, and also should reveal what "wrong" things are happening (such as variables with wrong values).
| 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 .
An interesting or useful task is important for many people, myself included.
For learning the shell, I suggest that you try trivial but useful _to_ _you_ tasks, at the command prompt. Not everything needs to be in a script. Anything you can put in a script you can type at the prompt, pretty much.
So, perhaps you find youself needing to do something to several files. Such as renaming them all to "thing.txt", when before they were called "thing".
Laboriously you can go:
$ mv thing1 thing1.txt $ mv thing2 thing2.txt $ mv thing3 thing3.txt
and so on. Or you can go:
$ for f in thing*; do mv $f $f.txt; done
Learning to use the basic control structures (such as the "for" loop above) in trivial convenient steps will get you started. Then it is easier to pick up the more advanced stuff later, as you find the need.
In short: try to do stuff at the shell prompt. When something is tedious, _then_ look for a way to make it easier, for just that task.
Cheers,
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 Simpsoncs@zip.com.au:
On 07Mar2011 10:17, xinyou yanyxy.716@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 Simpsoncs@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@lists.fedoraproject.org To unsubscribe or change subscription options: https://admin.fedoraproject.org/mailman/listinfo/users Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines