Skip to content

Instantly share code, notes, and snippets.

@treyharris
Created October 6, 2017 21:40
Show Gist options
  • Save treyharris/d914c6e3a3b0b0049d171daef1fc9c31 to your computer and use it in GitHub Desktop.
Save treyharris/d914c6e3a3b0b0049d171daef1fc9c31 to your computer and use it in GitHub Desktop.
# -*-shell-script-*-
# Prompt user for info but don't accept the empty string
#
# TODO 2015-02-12: generalize; make a version that will
# accept empty, will loop until it gets non-empty, etc.
function promptNonNull () {
# args:
# prompt (string) User prompt. Append a trailing space if desired.
# variable (varname) Name of variable in which to store response.
local answer="${(P)2}"
vared -p "$1" -c answer
if [[ -n "${answer}" ]]; then
typeset -g "${2}"="${answer}"
else
echoerr "$0: null response setting $2."
return 1
fi
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment