How to re-run the case statement if the input is invalid? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
This question already has an answer here:
How to repeat prompt to user in a shell script?
2 answers
I have the following code in the middle of a script to confirm whether we want to resume the script or not.
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
I would like to know is there any way to know is there any way to recall the switch-case if the input option is invalid?
linux bash shell-script case
marked as duplicate by Jeff Schaller, G-Man, Thomas, SivaPrasath, Romeo Ninov Sep 4 at 11:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
6
down vote
favorite
This question already has an answer here:
How to repeat prompt to user in a shell script?
2 answers
I have the following code in the middle of a script to confirm whether we want to resume the script or not.
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
I would like to know is there any way to know is there any way to recall the switch-case if the input option is invalid?
linux bash shell-script case
marked as duplicate by Jeff Schaller, G-Man, Thomas, SivaPrasath, Romeo Ninov Sep 4 at 11:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You need to rerun the switch case if the input is other thanY and N
, am i right?
â SivaPrasath
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
1
If it's bash, you can useselect
statement - that one doesn't exit until you have valid input, IIRC
â Sergiy Kolodyazhnyy
Sep 3 at 18:47
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
This question already has an answer here:
How to repeat prompt to user in a shell script?
2 answers
I have the following code in the middle of a script to confirm whether we want to resume the script or not.
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
I would like to know is there any way to know is there any way to recall the switch-case if the input option is invalid?
linux bash shell-script case
This question already has an answer here:
How to repeat prompt to user in a shell script?
2 answers
I have the following code in the middle of a script to confirm whether we want to resume the script or not.
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
I would like to know is there any way to know is there any way to recall the switch-case if the input option is invalid?
This question already has an answer here:
How to repeat prompt to user in a shell script?
2 answers
linux bash shell-script case
edited Sep 3 at 17:18
Kusalananda
105k14209326
105k14209326
asked Sep 3 at 16:27
user308897
marked as duplicate by Jeff Schaller, G-Man, Thomas, SivaPrasath, Romeo Ninov Sep 4 at 11:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Jeff Schaller, G-Man, Thomas, SivaPrasath, Romeo Ninov Sep 4 at 11:43
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You need to rerun the switch case if the input is other thanY and N
, am i right?
â SivaPrasath
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
1
If it's bash, you can useselect
statement - that one doesn't exit until you have valid input, IIRC
â Sergiy Kolodyazhnyy
Sep 3 at 18:47
add a comment |Â
1
You need to rerun the switch case if the input is other thanY and N
, am i right?
â SivaPrasath
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
1
If it's bash, you can useselect
statement - that one doesn't exit until you have valid input, IIRC
â Sergiy Kolodyazhnyy
Sep 3 at 18:47
1
1
You need to rerun the switch case if the input is other than
Y and N
, am i right?â SivaPrasath
Sep 3 at 16:30
You need to rerun the switch case if the input is other than
Y and N
, am i right?â SivaPrasath
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
1
1
If it's bash, you can use
select
statement - that one doesn't exit until you have valid input, IIRCâ Sergiy Kolodyazhnyy
Sep 3 at 18:47
If it's bash, you can use
select
statement - that one doesn't exit until you have valid input, IIRCâ Sergiy Kolodyazhnyy
Sep 3 at 18:47
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
12
down vote
Do your input in a loop. Exit the loop with break
(or exit
as the case may be) if you get a valid response from the user.
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
As a utility function:
ask_continue ()
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D):
ask_continue ()
while read -p 'Continue? yes/no: ' input; do
case $input in
[yY]*)
echo 'Continuing'
return
;;
[nN]*)
break
;;
*)
echo 'Invalid input' >&2
esac
done
echo 'Ok, exiting'
exit 1
Here, there are three ways out of the loop:
- The user enters "yes", in which case the function returns.
- The user enters "no", in which case the we
break
out of the loop and executeexit 1
. - The
read
fails due to something like encountering an end-of-input or some other error, in which case theexit 1
is executed.
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
add a comment |Â
up vote
6
down vote
Why not just repeating the read?
unset i
while [[ ! "$i" =~ ^[yYnN]$ ]]; do read -r -p "Would you like to continue [Y/N] : " i; done
add a comment |Â
up vote
3
down vote
You can do by keeping switch case inside a function.
function testCase ()
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
testCase
;;
esac
testCase
If the input is invalid it will recall the function until it gets a valid input.
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
add a comment |Â
up vote
1
down vote
until [ "$i" = "0" ]
do
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
done
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
12
down vote
Do your input in a loop. Exit the loop with break
(or exit
as the case may be) if you get a valid response from the user.
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
As a utility function:
ask_continue ()
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D):
ask_continue ()
while read -p 'Continue? yes/no: ' input; do
case $input in
[yY]*)
echo 'Continuing'
return
;;
[nN]*)
break
;;
*)
echo 'Invalid input' >&2
esac
done
echo 'Ok, exiting'
exit 1
Here, there are three ways out of the loop:
- The user enters "yes", in which case the function returns.
- The user enters "no", in which case the we
break
out of the loop and executeexit 1
. - The
read
fails due to something like encountering an end-of-input or some other error, in which case theexit 1
is executed.
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
add a comment |Â
up vote
12
down vote
Do your input in a loop. Exit the loop with break
(or exit
as the case may be) if you get a valid response from the user.
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
As a utility function:
ask_continue ()
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D):
ask_continue ()
while read -p 'Continue? yes/no: ' input; do
case $input in
[yY]*)
echo 'Continuing'
return
;;
[nN]*)
break
;;
*)
echo 'Invalid input' >&2
esac
done
echo 'Ok, exiting'
exit 1
Here, there are three ways out of the loop:
- The user enters "yes", in which case the function returns.
- The user enters "no", in which case the we
break
out of the loop and executeexit 1
. - The
read
fails due to something like encountering an end-of-input or some other error, in which case theexit 1
is executed.
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
add a comment |Â
up vote
12
down vote
up vote
12
down vote
Do your input in a loop. Exit the loop with break
(or exit
as the case may be) if you get a valid response from the user.
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
As a utility function:
ask_continue ()
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D):
ask_continue ()
while read -p 'Continue? yes/no: ' input; do
case $input in
[yY]*)
echo 'Continuing'
return
;;
[nN]*)
break
;;
*)
echo 'Invalid input' >&2
esac
done
echo 'Ok, exiting'
exit 1
Here, there are three ways out of the loop:
- The user enters "yes", in which case the function returns.
- The user enters "no", in which case the we
break
out of the loop and executeexit 1
. - The
read
fails due to something like encountering an end-of-input or some other error, in which case theexit 1
is executed.
Do your input in a loop. Exit the loop with break
(or exit
as the case may be) if you get a valid response from the user.
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
As a utility function:
ask_continue ()
while true; do
read -p 'Continue? yes/no: ' input
case $input in
[yY]*)
echo 'Continuing'
break
;;
[nN]*)
echo 'Ok, exiting'
exit 1
;;
*)
echo 'Invalid input' >&2
esac
done
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D):
ask_continue ()
while read -p 'Continue? yes/no: ' input; do
case $input in
[yY]*)
echo 'Continuing'
return
;;
[nN]*)
break
;;
*)
echo 'Invalid input' >&2
esac
done
echo 'Ok, exiting'
exit 1
Here, there are three ways out of the loop:
- The user enters "yes", in which case the function returns.
- The user enters "no", in which case the we
break
out of the loop and executeexit 1
. - The
read
fails due to something like encountering an end-of-input or some other error, in which case theexit 1
is executed.
edited Sep 4 at 5:58
answered Sep 3 at 16:41
Kusalananda
105k14209326
105k14209326
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
add a comment |Â
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
You may also want to exit the script upon empty input (EOF).
â Stéphane Chazelas
Sep 3 at 22:14
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
@StéphaneChazelas Yes, definitely. I'll update in the morning. Thanks.
â Kusalananda
Sep 3 at 22:45
add a comment |Â
up vote
6
down vote
Why not just repeating the read?
unset i
while [[ ! "$i" =~ ^[yYnN]$ ]]; do read -r -p "Would you like to continue [Y/N] : " i; done
add a comment |Â
up vote
6
down vote
Why not just repeating the read?
unset i
while [[ ! "$i" =~ ^[yYnN]$ ]]; do read -r -p "Would you like to continue [Y/N] : " i; done
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Why not just repeating the read?
unset i
while [[ ! "$i" =~ ^[yYnN]$ ]]; do read -r -p "Would you like to continue [Y/N] : " i; done
Why not just repeating the read?
unset i
while [[ ! "$i" =~ ^[yYnN]$ ]]; do read -r -p "Would you like to continue [Y/N] : " i; done
answered Sep 3 at 16:46
RudiC
1,1616
1,1616
add a comment |Â
add a comment |Â
up vote
3
down vote
You can do by keeping switch case inside a function.
function testCase ()
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
testCase
;;
esac
testCase
If the input is invalid it will recall the function until it gets a valid input.
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
add a comment |Â
up vote
3
down vote
You can do by keeping switch case inside a function.
function testCase ()
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
testCase
;;
esac
testCase
If the input is invalid it will recall the function until it gets a valid input.
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can do by keeping switch case inside a function.
function testCase ()
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
testCase
;;
esac
testCase
If the input is invalid it will recall the function until it gets a valid input.
You can do by keeping switch case inside a function.
function testCase ()
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
testCase
;;
esac
testCase
If the input is invalid it will recall the function until it gets a valid input.
answered Sep 3 at 16:33
SivaPrasath
1
1
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
add a comment |Â
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
3
3
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Or until you run into a resource restriction or a maximum recursion depth limit.
â Kusalananda
Sep 3 at 16:35
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
Thanks a lot it works!!
â user308897
Sep 3 at 16:42
8
8
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
I wouldn't count on the shell being able to optimize the tail recursion, and given the usual procedural nature of shell scripts, I'd really suggest writing that loop out.
â ilkkachu
Sep 3 at 18:33
1
1
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Kusalananda If the user gives an incorrect answer to a simple yes/no question so many times you hit the recursion limit, he deserves to have the script crash.
â Barmar
Sep 3 at 21:45
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
@Barmar A programmer should expect that the user will input anything, whether it's an incorrect answer enough times to hit a recursion limit, or eight gigabytes of null and control codes. Failing on a corner case like this is indicative of bad programming.
â forest
Sep 4 at 0:47
add a comment |Â
up vote
1
down vote
until [ "$i" = "0" ]
do
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
done
add a comment |Â
up vote
1
down vote
until [ "$i" = "0" ]
do
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
done
add a comment |Â
up vote
1
down vote
up vote
1
down vote
until [ "$i" = "0" ]
do
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
done
until [ "$i" = "0" ]
do
read -r -p "Would you like to continue [Y/N] : " i
case $i in
[yY])
echo -e "Resuming the script";;
[nN])
echo -e "Skipped and exit script"
exit 1;;
*)
echo "Invalid Option"
;;
esac
done
answered Sep 3 at 18:21
GAD3R
22.7k154895
22.7k154895
add a comment |Â
add a comment |Â
1
You need to rerun the switch case if the input is other than
Y and N
, am i right?â SivaPrasath
Sep 3 at 16:30
Sivaprasath, Yes
â user308897
Sep 3 at 16:30
Use functions from the main level of your script so that you can control flow more easily.
â slmâ¦
Sep 3 at 16:31
1
If it's bash, you can use
select
statement - that one doesn't exit until you have valid input, IIRCâ Sergiy Kolodyazhnyy
Sep 3 at 18:47