LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I make my BASH script yes/no user input query terminate with 3 invalid inputs? (http://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-make-my-bash-script-yes-no-user-input-query-terminate-with-3-invalid-inputs-795723/)

lupusarcanus 03-16-2010 05:33 AM

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

jamescondron 03-16-2010 05:36 AM

Without doing it for you, a pseudo'd version:
Code:

err=0
while err < 3{
  if valid{
    break
  } else {
    err++


lupusarcanus 03-16-2010 05:46 AM

Quote:

Originally Posted by jamescondron (Post 3900095)
Without doing it for you, a pseudo'd version:
Code:

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
...

Thanks for your informative reply.

penguiniator 03-16-2010 06:04 AM

Code:

(( err++ ))
The increment code should be in double parentheses. Make sure there are spaces separating the parentheses from the expression on both sides.

jamescondron 03-16-2010 06:17 AM

Yes, mine was pseudocode, not bash. It will error.

lupusarcanus 03-16-2010 06:25 AM

Quote:

Originally Posted by jamescondron (Post 3900151)
Yes, mine was pseudocode, not bash. It will error.

???

I'm a newbie at BASH scripting...

penguiniator 03-16-2010 06:35 AM

http://mywiki.wooledge.org/BashGuide
http://bash-hackers.org/wiki/doku.php

Here are a couple of links that may help you.

jamescondron 03-16-2010 07:49 AM

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

onebuck 03-16-2010 04:40 PM

Hi,

I like the Wikipedia definition;

Quote:

excerpt from Pseudocode;

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.
:hattip:

onebuck 03-16-2010 04:43 PM

Hi,

Just a few useful links;

Linux Documentation Project
Rute Tutorial & Exposition
Linux Command Guide
Bash Reference Manual
Advanced Bash-Scripting Guide
Linux Newbie Admin Guide
LinuxSelfHelp
Getting Started with Linux

These links and others can be found at 'Slackware-Links' .
More than just SlackwareŽ links!


All times are GMT -5. The time now is 06:11 PM.