Skip to content

Instantly share code, notes, and snippets.

@rhwood
Created December 1, 2015 17:47
Show Gist options
  • Save rhwood/40bdbced50689b8fff34 to your computer and use it in GitHub Desktop.
Save rhwood/40bdbced50689b8fff34 to your computer and use it in GitHub Desktop.
Editor completion in bash (defaulting to using vi)
#!/bin/sh
INSTRUCTIONS="-- This line, and those below, will be ignored. --
Add a business case. The business case should be described briefly in a single
line on the first line, followed by a more detailed description on the
following lines."
CONTENT=""
EDITOR=${EDITOR:-}
VISUAL=${VISUAL:-}
if [[ -z ${VISUAL} && -z ${EDITOR} ]] ; then
VISUAL=vi
elif [[ -z ${VISUAL} && ! -z ${EDITOR} ]] ; then
VISUAL=${EDITOR}
fi
until [[ ${CONTENT} =~ [[:alnum:]] ]] ; do
TMP_CONTENT=$( mktemp )
echo "" > ${TMP_CONTENT}
echo "${INSTRUCTIONS}" >> ${TMP_CONTENT}
${VISUAL} ${TMP_CONTENT}
CONTENT=$( cat ${TMP_CONTENT} )
CONTENT="${CONTENT%%${INSTRUCTIONS}}"
if [[ ! ${CONTENT} =~ [[:alnum:]] ]] ; then
read -p "No business case entered. Continue or Abort (C/A)? [Abort]: " RESPONSE
if [[ -z ${RESPONSE} || ! ${RESPONSE} =~ ^[cC] ]] ; then
exit
fi
fi
rm ${TMP_CONTENT}
done
echo "${CONTENT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment