1

I have a bash script which gives some output. But sometimes the output takes a very long time. How it is possible to like just wait for the output command for 5 seconds, if there is no output within 5secs we execute bash-script-2. If there is output we execute bash-script-1.

I tried searching the web couldn't get anything relevant.

0

1 Answer 1

2

Send the output to a log file, and then tail -f the log file with a timeout and match for anything:

some-long-running-command > log-file &
if timeout 5 tail -f log-file | grep .
then
  # grep found something before tail was killed
  bash-script-1
else
  # tail was killed before grep found anything
  bash-script-2
fi

The timeout command is from GNU coreutils.

You must log in to answer this question.

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