413.html
来自「著名的linux英雄站点的文档打包」· HTML 代码 · 共 390 行 · 第 1/2 页
HTML
390 行
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=10 cellPadding=0 width="100%" bgColor=#ffffff
border=0>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%" border=0>
<TR>
<TD vAlign=top align=middle width="60%">
<TABLE cellSpacing=0 cellPadding=0 width="100%"
background="images/back.gif" tppabs="http://www.linuxhero.com/docs/images/back.gif" border=0>
<TBODY>
<TR>
<TD vAlign=top width="80%">
<DIV align=center>
<FORM action="search.html" tppabs="http://www.linuxhero.com/docs/search.html" method=get>
</FORM>
<TABLE cellSpacing=0 cellPadding=0 width="95%"
border=0><TBODY>
<TR>
<TD background="images/bgi.gif" tppabs="http://www.linuxhero.com/docs/images/bgi.gif"
height=30></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 width="95%"
align=center border=0>
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD vAlign=top>
<p><FONT class=normalfont><B><font color=blue>shell 编程中的信号处理(signal handling in shell programming)</font></B></FONT><BR><FONT class=smallfont color=#ff9900>2004-04-23 15:18 pm</FONT><BR><FONT class=normalfont>作者:作者<br>来自:Linux知识宝库<br>联系方式:无名<br><br> 在有些情况下,我们不希望自己的shell脚本在运行时刻被中断,比如说我们写得shell脚<br>
本设为某一用户的默认shell,使这一用户进入系统后只能作某一项工作,如数据库备份, 我<br>
们可不希望用户使用ctrl+C之类便进入到shell状态,做我们不希望做的事情。这便用到了信号<br>
处理。<br>
<br>
kill -l可以列出系统的信号名称,如下:<br>
HUP INT QUIT<br>
ILL TRAP ABRT<br>
EMT FPE KILL<br>
BUS SEGV SYS<br>
PIPE ALRM TERM<br>
USR1 USR2 CHLD<br>
PWR WINCH URG<br>
POLL STOP TSTP<br>
CONT TTIN TTOU<br>
VTALRM PROF XCPU<br>
XFSZ<br>
共31个,在很多情况下,信号可以用序号也可以用名称表示。<br>
通常我们需要忽略的信号有四个,即:HUP, INT, QUIT, TSTP,也就是信号1, 2, 3, 24<br>
使用这样的语句可以使这些中断信号被忽略:<br>
trap "" 1 2 3 24 或 trap "" HUP INT QUIT TSTP<br>
用 trap :1 2 3 24 或 trap HUP INT QUIT TSTP使其回复默认值。<br>
用stty -a可以列出中断信号与键盘的对应,分别执行上面的命令后,运行<br>
tail -f /etc/passwd, 然后尝试用键盘中断,试试两种情况(默认和忽略)下有何不同。<br>
更方便的是我们可以用在shell中用trap定义我们自己的信号处理程序,就象在c中用<br>
signal一样,<br>
<br>
如:trap “echo 'GO Away'” INT<br>
或<br>
cleanup()<br>
{<br>
[font="" size=3] echo " catching signal..."<br>
rm /tmp/tmpfile<br>
exit<br>
}<br>
...<br>
trap clearnup INT TERM<br>
...<br>
这样使我们的程序在退出时可以清理一些临时文件。<br>
command list:<br>
stty kill trap<br>
<br>
[/home/guest]kill -l<br>
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL<br>
5) SIGTRAP 6) SIGABRT 7) SIGEMT SIGFPE<br>
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS<br>
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG<br>
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD<br>
21) SIGTTIN 22) SIGTTOU 23) SIGIO 24) SIGXCPU<br>
25) SIGXFSZ 27) SIGMSG 28) SIGWINCH 29) SIGPWR<br>
30) SIGUSR1 31) SIGUSR2 32) SIGPROF 33) SIGDANGER<br>
34) SIGVTALRM 35) SIGMIGRATE 36) SIGPRE 37) SIGVIRT<br>
38) SIGALRM1 39) SIGWAITING 60) SIGKAP 61) SIGRETRACT<br>
62) SIGSOUND 63) SIGSAK<br>
<br>
不同的系统不一样,先前是sco5.0.5上的结果,<br>
下面是HP-UX的结果,<br>
1) HUP 16) USR1 31) RESERVED<br>
2) INT 17) USR2 32) DIL<br>
3) QUIT 18) CHLD 33) XCPU<br>
4) ILL 19) PWR 34) XFSZ<br>
5) TRAP 20) VTALRM 35) bad trap<br>
6) IOT 21) PROF 36) bad trap<br>
7) EMT 22) POLL 37) RTMIN<br>
8) FPE 23) WINCH 38) RTMIN+1<br>
9) KILL 24) STOP 39) RTMIN+2<br>
10) BUS 25) TSTP 40) RTMIN+3<br>
11) SEGV 26) CONT 41) RTMAX-3<br>
12) SYS 27) TTIN 42) RTMAX-2<br>
13) PIPE 28) TTOU 43) RTMAX-1<br>
14) ALRM 29) URG 44) RTMAX<br>
15) TERM 30) LOST<br>
<br>
下面是cygwin下的结果:<br>
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL<br>
5) SIGTRAP 6) SIGABRT 7) SIGEMT SIGFPE<br>
9) SIGKILL10) SIGBUS11) SIGSEGV12) SIGSYS<br>
13) SIGPIPE14) SIGALRM15) SIGTERM16) SIGURG<br>
17) SIGSTOP18) SIGTSTP19) SIGCONT20) SIGCHLD<br>
21) SIGTTIN22) SIGTTOU23) SIGIO24) SIGXCPU<br>
25) SIGXFSZ26) SIGVTALRM27) SIGPROF28) SIGWINCH<br>
29) SIGLOST30) SIGUSR131) SIGUSR2<br>
大同小异。<br>
<br>
高呀。请教一个问题。<br>
我做了这样一个SHELL:<br>
trap "echo """ 2 3 4<br>
echo "enter date"<br>
read fdate<br>
flen=`echo $fdate|awi '{print $0}'`<br>
if [ $flen -ne 8 ]<br>
:<br>
:<br>
这个SHELL 在某些机器上运行到 IF 语句时出错,提示:TEST 。。。<br>
如果把TRAP语句屏蔽掉则可以在所有机器上正常运行。<br>
这是怎么回事???<br>
<br>
To fanyan1999 :<br>
你的if 语句在用户输入为空是一般会出错的,应该在此以前有如此形式的判断<br>
[ "X"$flen = "X" ] && echo "no input" && exit<br>
你的trap语句是不是可以多换的形式试一试。<br>
如:<br>
trap “echo ‘’” 2 3 4<br>
<br>
说明一下你用的系统和详细错误信息。<br>
<br>
请问一下<br>
trap "echo logout" 0<br>
这是什么意思?<br>
<br>
>> trap "echo logout" 0<br>
>> 这是什么意思?<br>
<br>
用户退出时执行 echo logout ,就是说用户退出时会看到 logout 的提示信息<br>
<br>
shell 编程中的信号处理(signal handling in shell programming)<br>
trap可以屏蔽信号2,15等,<br>
但屏蔽信号9好像不行。<br>
</FONT><br>
</TD>
</TR>
<TR>
<TD colSpan=2><FONT
class=middlefont></FONT><BR>
<FONT
class=normalfont>全文结束</FONT> </TD>
</TR>
<TR>
<TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2
height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="20%"
background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2>
<DIV align=center>
<table class=tableoutline cellspacing=1 cellpadding=4
width="100%" align=center border=0>
<tr class=firstalt>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
<font class=normalfont><b>所有分类</b></font></td>
</tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td> </tr> </table></td></tr> </table>
</DIV></TD></TR>
<TR vAlign=top>
<TD width="80%">
<DIV align=center><BR>
</DIV>
</TD></TR></TBODY></TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee
border=0><TBODY>
<TR>
<TD width="50%">
<P><FONT class=middlefont>版权所有 © 2004 <A
href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
违者必究. </FONT></P>
</TD>
<TD width="50%">
<DIV align=right><FONT class=middlefont>Powered by: <A
href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
</TD></TR></TBODY></TABLE>
<CENTER></CENTER></TD></TR>
</TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?