LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-16-2010, 04:33 AM   #1
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
Question 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
 
Old 03-16-2010, 04:36 AM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Without doing it for you, a pseudo'd version:
Code:
err=0
while err < 3{
  if valid{
    break
  } else {
    err++
 
1 members found this post helpful.
Old 03-16-2010, 04:46 AM   #3
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by jamescondron View Post
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.
 
Old 03-16-2010, 05:04 AM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Code:
(( err++ ))
The increment code should be in double parentheses. Make sure there are spaces separating the parentheses from the expression on both sides.
 
Old 03-16-2010, 05:17 AM   #5
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Yes, mine was pseudocode, not bash. It will error.
 
Old 03-16-2010, 05:25 AM   #6
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by jamescondron View Post
Yes, mine was pseudocode, not bash. It will error.
???

I'm a newbie at BASH scripting...
 
Old 03-16-2010, 05:35 AM   #7
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

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

Here are a couple of links that may help you.
 
Old 03-16-2010, 06:49 AM   #8
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
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
 
Old 03-16-2010, 03:40 PM   #9
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,146
Blog Entries: 3

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
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.
 
Old 03-16-2010, 03:43 PM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 11,146
Blog Entries: 3

Rep: Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406Reputation: 1406
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!
 
  


Reply

Tags
bash, input, invalid, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script predefined user input loopstr Programming 6 08-10-2009 01:10 PM
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Programming 10 09-07-2008 03:15 PM
calling user input in bash script dbmacartney Linux - General 3 05-19-2008 06:55 AM
mask user input in a bash script PlatinumRik Linux - Software 1 06-15-2004 10:06 AM
User input using a BASH script... causticmtl Programming 5 07-13-2003 09:59 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration