Scenario: You are running a code deployment to a small server farm of maybe 10-15 servers. Too small for something like tentakal, but too big to just ssh and deploy, logout, wash - rinse - repeat.
So you write a simple while; do loop and roll your stuff out that way.
The problem comes up when you realize that the deployment is hanging on a particular function, and you want to exit from that session, but want to continue your deployment on the rest of the machines - or whatever...
Enter my friend "trap".
Trap can pick up a variety of signals, and act on those in whatever way you wish.
Example: You want to stop someone from hitting ^C:
#!/bin/bash
trap "echo 'tsk tsk - no ^cing!'" 2
while true; do
echo "I dare you to press ^C"
echo ""
sleep 15
done
So - this will wait for the signal 2 (INTerrupt), and print tsk tsk - no ^cing! to the screen everytime you hit ^C.
Now, how do you stick this into a script so that it will execute that portion until it gets a ^C?
#!/bin/bash
# some code.. do what you want here...
#Trap code:
# First trap - if ctl-c is hit, it will go to the next function:
while [ trap -ne 2 ] ; do
# your commandline/script code here
done
# The next function:
while [ trap -ne 2 ] ; do
# your commandline/script code here
done
...and so on...