Created
August 30, 2012 15:05
-
-
Save Rondom/3530333 to your computer and use it in GitHub Desktop.
ask_variable (bash-only)
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
#!/bin/bash | |
# 1. Text | |
# 2. variableName | |
# 3. additonal parameters for read | |
function ask_variable() { | |
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