Created
August 5, 2011 09:33
-
-
Save Rondom/1127198 to your computer and use it in GitHub Desktop.
A helper function for interactive shell-scripts asking for a variable from keyboard. Fallback provided for bash 3
This file contains 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
# 1. Text | |
# 2. variableName | |
# 3. additonal parameters for read | |
function askVariable() { | |
askText="$1" | |
varName="$2" | |
eval default="\$$varName" | |
bashVersionMajor=$(echo ${BASH_VERSION:-3.0.0} | cut -d "." -f1) | |
if [ "$bashVersionMajor" -ge "4" ]; then | |
# use -i for bash-versions >= 4 | |
read $3 -e -p "$askText > " -i "$default" "$varName" | |
else | |
# set to default when string is empty for older bashs | |
read $3 -e -p "$askText [$default]> " "$varName" | |
eval "$varName=\${$varName:-$default}" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment