I am trying to create a small script for creating simple, all-default Apache virtual host files (it should be used any time I establish a new web application).
This script prompts me for the domain.tld of the web application and also for its database credentials, in verified read
operations:
read -p "Have you created db credentials already?" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please create db credentials and then comeback;";;
esac
read -p "Please enter the domain of your web application:" domain_1 && echo
read -p "Please enter the domain of your web application again:" domain_2 && echo
if [ "$domain_1" != "$domain_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi
read -sp "Please enter the app DB root password:" dbrootp_1 && echo
read -sp "Please enter the app DB root password again:" dbrootp_2 && echo
if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi
read -sp "Please enter the app DB user password:" dbuserp_1 && echo
read -sp "Please enter the app DB user password again:" dbuserp_2 && echo
if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched. Please try again." && exit 2; fi
Why I do it with Bash
As for now I would prefer Bash automation over Ansible automation because Ansible has a steep learning curve and its docs (as well as some printed book I bought about it) where not clear or useful for me in learning how to use it). I also prefer not to use Docker images and then change them after-build.
My problem
The entire Bash script (which I haven't brought here in its fullness) is a bit longer and the above "heavy" chuck of text makes it significantly longer - yet it is mostly a cosmetic issue.
My question
Is there an alternative for the verified read operations? A utility that both prompts twice and compares in one go?
Related: The need for $1 and $2 for comparison with an here-string
read
TMP1 and TMP2, into$2
how come there isn't a "clash" here and both still exist to be later checked by theif-then
? – Jan 01 '19 at 09:13$2
. The thing in$2
is used as part of the prompt. The variable whose name is in$1
is given the value of$TMP1
in the lastread
(which would be better writtenprintf -v "$1" '%s' "$TMP1"
). Also note that the positional parameters are local to the function and separate from the positional parameters in the main script. There is no clash between them. – Kusalananda Jan 01 '19 at 10:23