Skip to content

Instantly share code, notes, and snippets.

@geky
Last active April 7, 2022 15:40
Show Gist options
  • Save geky/fbc9111b6cab6cd2b3299143a68e83a6 to your computer and use it in GitHub Desktop.
Save geky/fbc9111b6cab6cd2b3299143a68e83a6 to your computer and use it in GitHub Desktop.
Runs the given markdown file as a bash script, with any failing commands resulting in an error
#!/bin/bash
# Runs the given markdown file as a bash script, with any failing commands
# resulting in an error
#
set -euo pipefail
if [ "$#" -lt 1 ]
then
echo "usage: $0 <file> [<filter>]" >&2
exit 1
fi
DOC="$1"
TERM="${2:-}"
# start from scratch
rm -f $DOC.sh
# grab every bash code block, remove line continuation, and only keep lines
# that start with '$' (of course removing that '$' in the process)
#
sed -n '/``` bash *'"$TERM"'/,/```/{/```/d; p}' $DOC \
| sed ':a; /\\$/{N; s/\\\n//; ta}' \
| sed -n '/^\$/{s/^\$ \?//; p}' \
>> $DOC.sh
# run script
bash -euxo pipefail $DOC.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment