I have the following bash compound command:

while true ; do slow-command-one ; slow-command-two ; slow-command-three ; done

What happens:

  • When I press Ctrl+C at any point, the entire command is aborted.

What I want to happen:

  • When I press Ctrl+C during execution of slow-command-two, slow-command-two should be aborted, and execution should continue with slow-command-three.
  • When I press Ctrl+C at any other time, the entire command should be aborted (as now).

How do I get this to happen?

link|improve this question

75% accept rate
2  
Ctrl-C is SIGINT. – Ignacio Vazquez-Abrams Nov 5 '10 at 11:03
Ta, have edited. – dave4420 Nov 5 '10 at 11:09
feedback

1 Answer

up vote 4 down vote accepted

You can use trap command for that. Catch SIGINT with it and Ctrl+C does not hurt your command execution. Then reset trap to default settings.

This should work:

#!/bin/bash

while true; do
  slow-command-one;
  trap "echo Proceeding to command three" SIGINT;
  slow-command-two;
  trap - SIGINT;
  slow-command-three;
done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.