I have a bash script that resumes aborted execution with a case statement. It works great with Bash 4 under CentOS 6 but does not work with CentOS 5 because of Bash 3.2.
Is there any programming logic I can use to replace the following with something more "portable"?
last-action=$(cat /tmp/last-action)
case last-action in)
beginning)
function1
;&
middle)
function2
;&
end)
function3
;&
esac
The thing is, wherever task execution stopped it must resume there and execute everything that comes after. For example, if last-action=middle
it would execute function2
and then function3
. That's where the operator ;&
came in handy.
Any clues to do this without a lot of if
statements?
I understand that fallthrough is very convenient, but given your example snippet I don't see how
case "${last_action}" in
beginning)
function1
function2
function3
;;
middle)
function2
function3
;;
end)
function3
;;
esac
would add much overhead to your code. It's still prettier than lots of if
s.
However, if your switch is more complex and you want this to be more dynamic, you can do something along the lines of:
#!/bin/bash
function1() { echo 1; }
function2() { echo 2; }
function3() { echo 3; }
last_action=$1
actions_beginning="function1 function2 function3"
actions_middle="function2 function3"
actions_end="function3"
_actions="actions_${last_action}"
for action in ${!_actions}; do
"${action}"
done
$ ./test.sh beginning
1
2
3
$ ./test.sh middle
2
3
$ ./test.sh end
3
EDIT: Just looked at your code on github and I myself would definitely go this route.