trap是对信号的处理,可以定义shell接收到哪个信号时,执行相应的操作。示例如下:
#! /bin/sh
trap "rm -f /tmp/my_file" INT
echo creating file "/tmp/my_file"
date > /tmp/my_file
echo press ctrl+c
while [ -f /tmp/my_file ]
do
echo exist
sleep 1
done
echo the file no longer exists
trap INT
echo creating file "/tmp/my_file"
date > /tmp/my_file
echo press ctrl+c
while [ -f /tmp/my_file ]
do
echo exist
sleep 1
done
echo wo never get there
exit 0