read-verification alternative (two prompts and if-then comparison alternative)
up vote
0
down vote
favorite
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?
shell-script docker read ansible case
This question has an open bounty worth +50
reputation from JohnDoea ending in 3 days.
This question has not received enough attention.
Please see edits of the question and read the answers and see if if you have anything to add as a new answer.
add a comment |
up vote
0
down vote
favorite
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?
shell-script docker read ansible case
This question has an open bounty worth +50
reputation from JohnDoea ending in 3 days.
This question has not received enough attention.
Please see edits of the question and read the answers and see if if you have anything to add as a new answer.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
shell-script docker read ansible case
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?
shell-script docker read ansible case
shell-script docker read ansible case
edited Nov 25 at 8:38
Kusalananda
118k16221360
118k16221360
asked Nov 23 at 4:30
JohnDoea
551132
551132
This question has an open bounty worth +50
reputation from JohnDoea ending in 3 days.
This question has not received enough attention.
Please see edits of the question and read the answers and see if if you have anything to add as a new answer.
This question has an open bounty worth +50
reputation from JohnDoea ending in 3 days.
This question has not received enough attention.
Please see edits of the question and read the answers and see if if you have anything to add as a new answer.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
How about a shell function? Like
function read_n_verify {
read -p "$2: " TMP1
read -p "$2 again: " TMP2
[ "$TMP1" != "$TMP2" ] &&
{ echo "Values unmatched. Please try again."; return 2; }
read "$1" <<< "$TMP1"
}
read_n_verify domain "Please enter the domain of your web application"
read_n_verify dbrootp "Please enter the app DB root password"
read_n_verify dbuserp "Please enter the app DB user password"
Then do your desired action/s with $domain, $dbrootp, $dbuserp.
Hi and thx, why is it$2and not$1(I do prompt another global variable as$1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?
– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiCThe positional parameters are local to the function and used for the variable nameI miss why not $1 even when they are local to the function...
– JohnDoea
Nov 26 at 10:04
add a comment |
up vote
1
down vote
I would do it like this:
#!/bin/bash
while [[ $string != 'string' ]] || [[ $string == '' ]]
do
read -p "Please enter the domain of your web application: " string
echo "Please enter the domain of your web application: "
done
Command 1
Command 2
Less typing.
Of course you would need a section like this for all of your questions.
Other than the way you have it and the way I have it, not really any more options.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
How about a shell function? Like
function read_n_verify {
read -p "$2: " TMP1
read -p "$2 again: " TMP2
[ "$TMP1" != "$TMP2" ] &&
{ echo "Values unmatched. Please try again."; return 2; }
read "$1" <<< "$TMP1"
}
read_n_verify domain "Please enter the domain of your web application"
read_n_verify dbrootp "Please enter the app DB root password"
read_n_verify dbuserp "Please enter the app DB user password"
Then do your desired action/s with $domain, $dbrootp, $dbuserp.
Hi and thx, why is it$2and not$1(I do prompt another global variable as$1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?
– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiCThe positional parameters are local to the function and used for the variable nameI miss why not $1 even when they are local to the function...
– JohnDoea
Nov 26 at 10:04
add a comment |
up vote
3
down vote
How about a shell function? Like
function read_n_verify {
read -p "$2: " TMP1
read -p "$2 again: " TMP2
[ "$TMP1" != "$TMP2" ] &&
{ echo "Values unmatched. Please try again."; return 2; }
read "$1" <<< "$TMP1"
}
read_n_verify domain "Please enter the domain of your web application"
read_n_verify dbrootp "Please enter the app DB root password"
read_n_verify dbuserp "Please enter the app DB user password"
Then do your desired action/s with $domain, $dbrootp, $dbuserp.
Hi and thx, why is it$2and not$1(I do prompt another global variable as$1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?
– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiCThe positional parameters are local to the function and used for the variable nameI miss why not $1 even when they are local to the function...
– JohnDoea
Nov 26 at 10:04
add a comment |
up vote
3
down vote
up vote
3
down vote
How about a shell function? Like
function read_n_verify {
read -p "$2: " TMP1
read -p "$2 again: " TMP2
[ "$TMP1" != "$TMP2" ] &&
{ echo "Values unmatched. Please try again."; return 2; }
read "$1" <<< "$TMP1"
}
read_n_verify domain "Please enter the domain of your web application"
read_n_verify dbrootp "Please enter the app DB root password"
read_n_verify dbuserp "Please enter the app DB user password"
Then do your desired action/s with $domain, $dbrootp, $dbuserp.
How about a shell function? Like
function read_n_verify {
read -p "$2: " TMP1
read -p "$2 again: " TMP2
[ "$TMP1" != "$TMP2" ] &&
{ echo "Values unmatched. Please try again."; return 2; }
read "$1" <<< "$TMP1"
}
read_n_verify domain "Please enter the domain of your web application"
read_n_verify dbrootp "Please enter the app DB root password"
read_n_verify dbuserp "Please enter the app DB user password"
Then do your desired action/s with $domain, $dbrootp, $dbuserp.
edited Nov 24 at 14:14
JohnDoea
551132
551132
answered Nov 23 at 17:27
RudiC
3,4121312
3,4121312
Hi and thx, why is it$2and not$1(I do prompt another global variable as$1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?
– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiCThe positional parameters are local to the function and used for the variable nameI miss why not $1 even when they are local to the function...
– JohnDoea
Nov 26 at 10:04
add a comment |
Hi and thx, why is it$2and not$1(I do prompt another global variable as$1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?
– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiCThe positional parameters are local to the function and used for the variable nameI miss why not $1 even when they are local to the function...
– JohnDoea
Nov 26 at 10:04
Hi and thx, why is it
$2 and not $1 (I do prompt another global variable as $1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?– JohnDoea
Nov 24 at 14:59
Hi and thx, why is it
$2 and not $1 (I do prompt another global variable as $1, you might assumed this is the reason?` Also, shouldn't the ``TMP` better be all lowercase not to clash with potential envars?– JohnDoea
Nov 24 at 14:59
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
Oh and why are the braces around the echo and return exit-code 2?
– JohnDoea
Nov 24 at 15:00
2
2
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
The positional parameters are local to the function and used for the variable name, and the prompt text, respectively. The braces enclose a "compound command" to be executed on a TRUE condition.
– RudiC
Nov 24 at 15:47
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
Passwords with trailing spaces and backslashes may be improperly read here.
– Kusalananda
Nov 25 at 8:55
@RudiC
The positional parameters are local to the function and used for the variable name I miss why not $1 even when they are local to the function...– JohnDoea
Nov 26 at 10:04
@RudiC
The positional parameters are local to the function and used for the variable name I miss why not $1 even when they are local to the function...– JohnDoea
Nov 26 at 10:04
add a comment |
up vote
1
down vote
I would do it like this:
#!/bin/bash
while [[ $string != 'string' ]] || [[ $string == '' ]]
do
read -p "Please enter the domain of your web application: " string
echo "Please enter the domain of your web application: "
done
Command 1
Command 2
Less typing.
Of course you would need a section like this for all of your questions.
Other than the way you have it and the way I have it, not really any more options.
add a comment |
up vote
1
down vote
I would do it like this:
#!/bin/bash
while [[ $string != 'string' ]] || [[ $string == '' ]]
do
read -p "Please enter the domain of your web application: " string
echo "Please enter the domain of your web application: "
done
Command 1
Command 2
Less typing.
Of course you would need a section like this for all of your questions.
Other than the way you have it and the way I have it, not really any more options.
add a comment |
up vote
1
down vote
up vote
1
down vote
I would do it like this:
#!/bin/bash
while [[ $string != 'string' ]] || [[ $string == '' ]]
do
read -p "Please enter the domain of your web application: " string
echo "Please enter the domain of your web application: "
done
Command 1
Command 2
Less typing.
Of course you would need a section like this for all of your questions.
Other than the way you have it and the way I have it, not really any more options.
I would do it like this:
#!/bin/bash
while [[ $string != 'string' ]] || [[ $string == '' ]]
do
read -p "Please enter the domain of your web application: " string
echo "Please enter the domain of your web application: "
done
Command 1
Command 2
Less typing.
Of course you would need a section like this for all of your questions.
Other than the way you have it and the way I have it, not really any more options.
edited Nov 26 at 13:49
answered Nov 24 at 15:14
Michael Prokopec
62115
62115
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483587%2fread-verification-alternative-two-prompts-and-if-then-comparison-alternative%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown