How do I make my BASH script yes/no user input query terminate with 3 invalid inputs?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I make my BASH script yes/no user input query terminate with 3 invalid inputs?
Hello all.
I have a BASH script which at one point asks the user a yes/no question. I want to make it so that if the user types in an invalid input 3 times consecutively then the BASH script will echo an error and terminate with exit status 1.
Here is my current code:-
Code:
...
while true
do
echo -n " Do you agree to these terms? (y/n): "
read CONFIRM
case $CONFIRM in
y|Y) break ;;
n|N)
echo "
ERROR 5: User declined the agreement.
"
exit
;;
*) echo "
ERROR 6: That is not vaild input.
"
esac
done
...
Could you please tell me how I could incorporate such a condition in my script?
Thanks in advance.
leopard
Last edited by lupusarcanus; 03-16-2010 at 04:37 AM.
Reason: fixed code elipses misplacement
err=0
while err < 3{
if valid{
break
} else {
err++
While it's most likely a syntax error on my part, the script failed.
The script failed at " } else {"
Here it is:-
Code:
...
while true
do
echo -n " Do you agree to these terms? (y/n): "
read CONFIRM
case $CONFIRM in
y|Y) break ;;
n|N)
echo "
ERROR 5: User declined the agreement.
"
exit
;;
*) echo "
ERROR 6: That is not vaild input.
"
err=0
while err < 3{
if valid{
break
} else {
echo "
ERROR 7: User exceeded 3 consecutive invaild input limit. Script terminated.
"
exit 1
}
esac
done
...
Pseudocode; break it into it's composite parts, pseudo and code. Code is simple, we know the definition of this. Pseudo means 'fake' or 'pretend' (Actually, it doesn't quite, but I'm not a linguist). Even better:
Code:
jc@jcmain:~$ dict pseudocode
1 definition found
From The Free On-line Dictionary of Computing (27 SEP 03) [foldoc]:
Pseudocode
A notation resembling a programming language but not intended
for actual compilation. It usually combines some of the
structure of a programming language with an informal
natural-language description of the computations to be carried
out. It is often produced by CASE systems as a basis for
later hand coding.
Basically I gave you a little snippet of this for two reasons, to aid you instead of doing it for you, but mainly because it was quicker than coding it myself.
What you need is to set a counter and increment it when an incorrect/invalid answer is given; then you can check this value and if it equals 3 you can return an 'Invalid Input' error
Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and subroutines. The programming language is augmented with natural language descriptions of the details, where convenient, or with compact mathematical notation. The purpose of using pseudocode is that it is easier for humans to understand than conventional programming language code, and that it is a compact and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place.
No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program. Pseudocode resembles, but should not be confused with, skeleton programs including dummy code, which can be compiled without errors. Flowcharts can be thought of as a graphical alternative to pseudocode.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.