Bash: how to interrupt this script when there's a CTRL-C?

Posted by WizardOfOdds on Stack Overflow See other posts from Stack Overflow or by WizardOfOdds
Published on 2010-03-31T09:03:55Z Indexed on 2010/03/31 9:23 UTC
Read the original article Hit count: 444

Filed under:
|
|

I wrote a tiny Bash script to find all the Mercurial changesets (starting from the tip) that contains the string passed in argument:

#!/bin/bash

CNT=$(hg tip | awk '{ print $2 }' | head -c 3)
while [ $CNT -gt 0 ]
do
    echo rev $CNT
    hg log -v -r$CNT | grep $1       
    let CNT=CNT-1
done

If I interrupt it by hitting ctrl-c, more often than not the command currently executed is "hg log" and it's that command that gets interrupted, but then my script continues.

I was then thinking of checking the return status of "hg log", but because I'm piping it into grep I'm not too sure as to how to go about it...

How should I go about exiting this script when it is interrupted? (btw I don't know if that script is good at all for what I want to do but it does the job and anyway I'm interested in the "interrupted" issue)

© Stack Overflow or respective owner

Related posts about bash

Related posts about interrupt