Skip to content

Instantly share code, notes, and snippets.

@Apsu
Created August 7, 2015 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apsu/763243a437bf45461acd to your computer and use it in GitHub Desktop.
Save Apsu/763243a437bf45461acd to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Set options
set -euo pipefail
# Initial step count
STEP=0
# Handle errors
handle_error() {
# Store command that caused error
cmd="$BASH_COMMAND"
# If we didn't run any steps yet
if [[ $STEP -eq 0 ]]; then
echo "Error before running steps: $cmd"
# Otherwise inform how to resume
else
echo "Error in step $STEP: $step"
echo "Please resume with: $0 $STEP"
fi
}
# Hook errors to handler
trap handle_error ERR
# Load up command steps from stdin
readarray -t -O 1 STEPS
# If no step arg specified
if [[ $# -lt 1 ]]; then
# Start at step 1
start=1
else
# Otherwise start at arg
start=$1
# Offset for counter ordering
STEP=$(($1-1))
fi
# Dispatch steps in order, from starting step
for step in "${STEPS[@]:$start}"; do
# Bump step-counter
STEP=$((STEP+1))
# Talk about it
echo "Running step $STEP: $step"
# Do the needful
eval $step
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment