Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sunaku
Last active July 30, 2020 02:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sunaku/f5f79e20b6f92ab1e524 to your computer and use it in GitHub Desktop.
Save sunaku/f5f79e20b6f92ab1e524 to your computer and use it in GitHub Desktop.
POSIX shell script equivalent of https://github.com/mplewis/shed
#!/bin/sh -e
#
# POSIX shell script equivalent of:
# <https://github.com/mplewis/shed>
#
# Usage: shed [SHELL_ARGUMENTS...]
#
# Executes stdin after you edit it.
# If $EDITOR is unset, uses $PAGER.
# If $PAGER is unset, uses cat(1).
#
# To execute stdin with OTHER shell:
#
# ln -s shed OTHERed
# ln -s shed fished # fish shell
# ln -s shed bashed # bash shell
# ln -s shed zshed # zsh shell
# ln -s shed kshed # ksh shell
# ln -s shed cshed # csh shell
# write stdin to a temporary file
temp=$(mktemp)
trap 'rm -f "$temp"' EXIT
trap 'exit' TERM INT
cat > "$temp"
exec 0</dev/tty
# edit or view the temporary file
${EDITOR:-${PAGER:-cat}} "$temp"
# confirm temporary file execution
printf 'Do you still want to execute this script? (yes/no): '
until read REPLY && echo "$REPLY" | grep -iq '^yes$'; do
echo "$REPLY" | grep -iq '^no$' && exit
printf 'Please respond with either "yes" or "no": '
done
# execute the temporary file
shell=$(basename "$0")
${shell%ed} "$@" "$temp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment