这非常适用:为什么陷阱信号在Shell函数中不起作用?
$./tailx.sh error.log 10.21.xxx.xxx # /tmp/.log.pipe is removed
但是,当这样执行/tmp/.log.pipe不会被删除:
$source tailx.sh
$tailx error.log 10.21.xxx.xxx # /tmp/.log.pipe is not removed
我想知道为什么和怎么样?
这是我的代码。我用它来远程机器上的日志。
#!/bin/bash
# tailx error.log hostname
function tailx {
[ $# -lt 2 ] && echo "Invalid input" && return
# do clean,
local LOG_PIPE=/tmp/.log.pipe
local LOG_FILE=$1
trap 'echo Exting..... >&2 && [ -e $LOG_PIPE ] && rm $LOG_PIPE ' EXIT
# fix path
[/!= ${LOG_FILE:0:1} ] && LOG_FILE=`pwd`"/"$LOG_FILE
[ -e $LOG_PIPE ] || mkfifo $LOG_PIPE
# iterate host, tail log
shift
until [ $# -eq 0 ]
do
ssh $1 "tail -f $LOG_FILE | awk 'BEGIN{\"hostname\"|getline HOST; } {print HOST, \$0}'" > $LOG_PIPE &
shift
done
cat $LOG_PIPE
}
tailx "[email protected]"
什么时候“EXIT”陷阱触发?手动运行脚本时何时退出?当您将脚本编入当前shell会话时何时退出? – 2014-10-03 11:32:44