Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Last active October 3, 2018 19:06
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 brianloveswords/16aa5af66d3ffe73242ef8dc6728a58f to your computer and use it in GitHub Desktop.
Save brianloveswords/16aa5af66d3ffe73242ef8dc6728a58f to your computer and use it in GitHub Desktop.
mkbin: a shell script that makes shell scripts
#!/bin/bash
# mkbin: create a new shell script and open the editor
mode="create"
if [ "$1" = "-e" ] || [ "$1" = "--edit" ]; then
mode="edit"
shift
fi
bin="$1"
fullpath="$HOME/bin/$1"
if [ -z "$bin" ]; then
echo "must pass bin name"
exit 1
fi
if [ "$mode" = "edit" ]; then
if [ ! -f "$fullpath" ]; then
echo "$fullpath does not exist"
exit 1
fi
"$EDITOR" "$fullpath" &
exit 1
fi
if [ -f "$fullpath" ]; then
echo "file exists"
exit 1
fi
cat >"$fullpath" <<EOF
#!/bin/bash
set -o errexit; set -o nounset;
# $1 - do stuff
EOF
chmod +x "$fullpath"
"$EDITOR" "$fullpath" &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment