14

I have a bash script, wherein I execute a line, sleep for sometime and then tail -f my log file to verify a certain pattern is seen, I press ctrl +c to get out of tail -f and then move to the next line till the bash script finishes execution:

Here is what I have done thus far:

#!/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

The script works but I get the error, what needs to change/ what am I doing wrong?

./script.sh: line 1: continue: only meaningful in a `for', `while', or `until' loop
1

2 Answers 2

12

The continue keyword doesn't mean whatever you think it means. It means continue to the next iteration of a loop. It makes no sense outside of a loop.

I think you're looking for

trap ' ' INT

Since you don't want to do anything upon reception of the signal (beyond killing the foreground job), put no code in the trap. You need a non-empty string, because the empty string has a special meaning: it causes the signal to be ignored.

3
  • 2
    Or trap : INT Commented Feb 10, 2015 at 23:21
  • Also note that the trap must be before the line you need to break
    – Amir Uval
    Commented Aug 3, 2020 at 6:21
  • 1
    @AmirUval not necessarily. It needs to be executed before the line you want to break Commented Apr 4, 2023 at 8:56
1

The error arise due to trap 'continue' SIGINT. From help trap:

ARG is a command to be read and executed when the shell receives the signal(s) SIGNAL_SPEC

So your script try to do continue command when receive SIGINT call but the continue is used in loops only.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .