Last active
April 7, 2022 15:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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