Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- . /etc/init.d/functions
- # Use step(), try(), and next() to perform a series of commands and print
- # [ OK ] or [FAILED] at the end. The step as a whole fails if any individual
- # command fails.
- #
- # Example:
- # step "Remounting / and /boot as read-write:"
- # try mount -o remount,rw /
- # try mount -o remount,rw /boot
- # next
- step() {
- echo -n "$@"
- STEP_OK=0
- [[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
- }
- try() {
- # Check for `-b' argument to run command in the background.
- local BG=
- [[ $1 == -b ]] && { BG=1; shift; }
- [[ $1 == -- ]] && { shift; }
- # Run the command.
- if [[ -z $BG ]]; then
- "$@"
- else
- "$@" &
- fi
- # Check if command failed and update $STEP_OK if so.
- local EXIT_CODE=$?
- if [[ $EXIT_CODE -ne 0 ]]; then
- STEP_OK=$EXIT_CODE
- [[ -w /tmp ]] && echo $STEP_OK > /tmp/step.$$
- if [[ -n $LOG_STEPS ]]; then
- local FILE=$(readlink -m "${BASH_SOURCE[1]}")
- local LINE=${BASH_LINENO[0]}
- echo "$FILE: line $LINE: Command \`$*' failed with exit code $EXIT_CODE." >> "$LOG_STEPS"
- fi
- fi
- return $EXIT_CODE
- }
- next() {
- [[ -f /tmp/step.$$ ]] && { STEP_OK=$(< /tmp/step.$$); rm -f /tmp/step.$$; }
- [[ $STEP_OK -eq 0 ]] && echo_success || echo_failure
- echo
- return $STEP_OK
- }
- prompt() {
- echo -n $@
- read -n1 val
- echo
- return $val
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement