LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-21-2010, 08:08 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Using loops in a Search


Code:
Title=
echo -n "Title: "
read Title
echo -n "Author:"
read Author




validation=`grep -i "$Title" fruit | grep -i "$Author"  | awk -F":" '{ print $1}'`


if [ "$Title" = "$validation" ] ; then
clear

echo "Error! Book Already Exist"

else
echo "You may input a new book in/

hey guys, im doing a simple search program and i came up with a problem. What i am trying to achieve is, if the search is false(means the book does exist) , the program will then ask the user to input the information again(title and author) and do a search again until there is no such book and then display a message saying he can input a new book in.


i tried using a until loop, but it does not seem to work. some guidance on how the statement should be and where should it be placed?

Last edited by gregarion; 01-21-2010 at 09:20 AM.
 
Old 01-21-2010, 09:20 AM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
You can use "while [ 1 ]" and "break" like that:
Code:
#!/bin/sh
while [ true ] 
do
	echo -n "Please enter the first number: "
	read first
	echo -n "Please enter the second number: "
	read second
	if [ "$first" -ne "$second" ]; then
		echo "The first number is different from the second number. Please try again!"
	else
		echo "Congratulation! The first number is equal to the second number."
		break
	fi
done
exit 0
 
Old 01-21-2010, 09:26 AM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Hey thank guanta, just to check, the "break" commands basically stops the loop from running again right? so basically if the numbers does equal, then the while command will read break which means it is true?
 
Old 01-22-2010, 01:34 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be >= 1. If n is greater than the number of enclosing loops, all enclosing loops are exited. The return value is 0 unless the shell is not executing a loop when break is executed.
http://linux.die.net/man/1/bash
 
Old 01-25-2010, 02:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just a side note on using break, as it is able to break out from any level of nested loops depending
on the number supplied, it can potentially make it very difficult to follow your code.

An alternative is to set a variable within your loop and test it to see what the value is:

ie. while [[ "x$TEST" != "xend" ]]
do
. . .
if [[ true_expression ]]
then
...
else
...
TEST=end
fi
done
 
  


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
Search tools (Affinity, Tracker Search Tool, etc.) not working - don't find any files Adamantus Linux - Newbie 1 03-29-2009 11:21 PM
possible search crash.../home/httpd/linuxquestions/questions/search.php aus9 LQ Suggestions & Feedback 3 09-06-2008 07:27 PM
Can you make search ...search a string in a link....a url...a web address aus9 LQ Suggestions & Feedback 4 04-16-2008 09:37 AM
Quick VIM question (unhighlighting search terms after search) lrt2003 Linux - Newbie 5 05-08-2004 05:21 PM
loops JMK Linux - Newbie 11 04-09-2004 05:30 PM

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

All times are GMT -5. The time now is 11:21 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