cog*_*1v3 11 shell bash signals trap
我有一个 bash 脚本,其中我执行一行,睡眠一段时间然后tail -f我的日志文件来验证某个模式被看到,我按 ctrl + c 退出tail -f然后移动到下一行,直到 bash 脚本完成执行:
这是我迄今为止所做的:
#!/bin/bash
# capture the hostname
host_name=`hostname -f`
# method that runs tail -f on log_file.log and looks for pattern and passes control to next line on 'ctrl+c'
echo "==================================================="
echo "On $host_name: running some command"
some command here
echo "On $host_name: sleeping for 5s"
sleep 5
# Look for: "pattern" in log_file.log
# trap 'continue' SIGINT
trap 'continue' SIGINT
echo "On $host_name: post update looking for pattern"
tail -f /var/log/hadoop/datanode.log | egrep -i -e "receiving.*src.*dest.*"
# some more sanity check
echo "On $host_name: checking uptime on process, tasktracker and hbase-regionserver processes...."
sudo supervisorctl status process
# in the end, enable the balancer
# echo balance_switch true | hbase shell
Run Code Online (Sandbox Code Playgroud)
该脚本有效,但出现错误,需要更改什么/我做错了什么?
./script.sh: line 1: continue: only meaningful in a `for', `while', or `until' loop
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 11
该continue关键字并不意味着不管你认为它的意思。这意味着继续循环的下一次迭代。在循环之外没有任何意义。
我想你正在寻找
trap ' ' INT
Run Code Online (Sandbox Code Playgroud)
由于您不想在接收到信号后做任何事情(除了终止前台工作),因此不要在陷阱中放置任何代码。你需要一个非空字符串,因为空字符串有一个特殊的含义:它会导致信号被忽略。