-
-
Save Micrified/2dbc96c8b70ea71b07603fa07beeafca to your computer and use it in GitHub Desktop.
Bash script that buffers standard output of given input command and enables operations upon it
This file contains hidden or 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/sh | |
# buf: Interactive facility for buffer operations | |
USAGE="$0 (<cmd> [<arg>]*) | -h(elp) | |
Summary: | |
Executes input command and stashes standard output | |
in buffer. Interactive prompt (#) supports shell | |
interaction with buffer with supporting builtin's | |
Interaction: | |
:! Re-run input command and overwrite buffer | |
:: Echo buffer contents to standard out | |
:q Exit | |
:<cmd> Run command with no standard input. Useful | |
for non-input commands | |
<cmd> Run command with buffer as standard input. | |
Output overwrites buffer if exit-code is | |
zero. Otherwise prompted to overwrite | |
" | |
# argument check | |
case $# in | |
0) echo "${USAGE}" >&2; exit 1 ;; | |
1) if [ "$1" == "-h" ]; then | |
echo "${USAGE}" >&2; exit 0 | |
fi ;; | |
esac | |
# evalulate | |
stdout=`"$@"` | |
echo "$stdout" | |
while :; do | |
printf "# " >&2 | |
read opt | |
case $opt in | |
:!) stdout=`"$@"`; echo "$@" "$?" >&2 ;; | |
::) echo "$stdout" ;; | |
:q) break ;; | |
:*) cmd=`echo "$opt" | sed 's/^://g'`; | |
$cmd 1>/dev/null 2>&2 | |
echo "$?" >&2 ;; | |
*) cmd=$opt | |
tmp=`eval "echo \"$stdout\" | $cmd"` 2>/dev/tty | |
ext=$? | |
if [ "$ext" -ne "0" ]; then | |
printf "bad exit code ($ext). Overwrite? (y/n): " >&2 | |
read opt | |
case opt in | |
y) stdout=$tmp | |
esac | |
else | |
stdout=$tmp | |
fi | |
echo "$stdout" >&2 ;; | |
esac | |
done | |
echo "$stdout" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment