LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2013, 05:01 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Rep: Reputation: 55
How can I force y/n answer in bash?


For example this code:

Code:
echo "Do you live in the US? (y/n)";
read confirm
if [ '$confirm' == 'y' ];then
  echo "In US";
fi
if [ '$confirm' == 'n' ];then
  echo "Outside US";
fi
How can I get it to keep looping and asking the question again if no answer is entered?

TIA
 
Old 09-16-2013, 05:07 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Just put it in a while loop
Code:
validans=0
while [[ $validans -eq 0 ]]; do
  echo "Do you live in the US? (y/n)"
  read confirm
  if [ "$confirm" == "y" ]; then
    echo "In US"
    validans=1
  fi
  if [ "$confirm" == "n" ]; then
    echo "Outside US"
    validans=1
  fi
done
 
1 members found this post helpful.
Old 09-16-2013, 05:29 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: lost+found
Distribution: CentOS
Posts: 1,430

Original Poster
Rep: Reputation: 55
That worked, thanks.
 
  


Reply



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
Can somebody answer a BASH question sigint-ninja Linux - General 9 08-05-2013 10:09 AM
How To Answer System Questions Inside A Bash Script ? zemon Programming 4 01-24-2011 06:07 PM
Probably easy to answer Bash question helptonewbie Programming 2 08-31-2006 05:30 PM
BASH: How to answer command prompts using redirects ?? swedish_lunacy Linux - Newbie 1 07-25-2006 02:28 AM
Bash Auto Answer Kedelfor Programming 1 11-23-2005 06:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration