免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1425 | 回复: 3
打印 上一主题 下一主题

关于trap [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-02-07 16:19 |只看该作者 |倒序浏览
以下一个小例子,运行了下,发现按ctrl+c根本没有清除临时文件,why?
ctrl+c 就是信号2吧,那么我怎么按ctrl+c根本没用。


以下代码保存为~/bin/sigtrap.sh
#!/bin/bash
#sigtrap
#A small script to demonstrate signal trapping
tmpFile=/tmp/sigtrap$$
cat > $tmpFile
function removeTemp() {
if [ -f  “$tmpFile” ]
then
echo “Sorting out the temp file... “
rm –f “$tmpFile”
fi
}
trap removeTemp 1 2
exit 0

$chmod 755 ~/bin/sigtrap.sh
$ ~/bin/sigtrap.sh

论坛徽章:
0
2 [报告]
发表于 2007-02-07 16:28 |只看该作者
sleep 1000放到exit 0前面看看

论坛徽章:
0
3 [报告]
发表于 2007-02-07 16:30 |只看该作者
  1. tmpFile=/tmp/sigtrap$$
  2. cat > $tmpFile
复制代码
这两句放到trap语句下面。

论坛徽章:
0
4 [报告]
发表于 2007-02-08 09:02 |只看该作者
只能写成这样的了。
#!/bin/bash
#sigtrap
#A small script to demonstrate signal trapping
tmpFile=/tmp/sigtrap$$
function removeTemp() {
echo "trap a signal"
if [ -f "$tmpFile" ]
then
echo "Sorting out the temp file..."
rm -f "$tmpFile"
fi
}
trap removeTemp 1 2
cat > $tmpFile
#trap "echo ccccccc" 1 2
exit 0

解释程序能不能智能一点呢?函数这些东东都必须预先声明吗?我发现不把function写在前面,trap就找不到了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP