Using loops in a search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using loops in a search
# 1  
Old 01-21-2010
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 11:21 AM..
# 2  
Old 01-21-2010
Hello Greg,

I did use while loop & below mentioned script worked as per your requirement. I have used a variable as flag to indicate success (or) error.

Code:
#!/bin/ksh

myVal=0

validateAuthor()
{
Title=
echo -n "Title: "
read Title
echo -n "Author:"
read Author
echo ""

validation=`grep -i "$Title" fruit | grep -i "$Author"  | awk -F":" '{ print $1}'`
TITLECHECK=` echo "$Title"  | tr "[:lower:]" "[:upper:]"`
VALIDATIONCHECK=` echo "$validation"  | tr "[:lower:]" "[:upper:]"`
if [ "$TITLECHECK" = "$VALIDATIONCHECK" ] ; then
clear
echo ""
echo ""
echo "Error! Book Already Exist"
myVal=0
else
myVal=1
echo "You may input a new book in"
fi
}

while [ $myVal -eq 0 ]
do
validateAuthor
done

Regards,
B.S. Nithin.

Last edited by pludi; 01-21-2010 at 12:28 PM.. Reason: code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

3. Homework & Coursework Questions

If and Loops

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: In this script you will take a directory as input from the user and change the end of line sequence from a Unix... (1 Reply)
Discussion started by: Pcarson
1 Replies

4. Shell Programming and Scripting

Help with loops

I am trying to do ftp of some crt files that have spaces in the file names. I have to do approximately 4500 files ftp'ed and then check whether the trasnfered file size matches the source files. This has to be in a loop I mean 1. ftp the file, check for file size whether the source and target... (0 Replies)
Discussion started by: dsravan
0 Replies

5. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

6. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

7. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

8. UNIX for Advanced & Expert Users

Loops

Can anybody help please. I am trying to right a script which will loop until a certain action has been performed. For example i current have two batch jobs i would like to put into a wait status. Batch Jobs A and B . The script i am trying to get to work is below. jobs="A B" COUNT=0 while... (2 Replies)
Discussion started by: mariner
2 Replies

9. Shell Programming and Scripting

no more loops

Hello Guys I need some help with my script I'm not very good at this, hope you can point me in the right direction. My idea, to write a file with some router commands and use it on a script for me to run on a demand basis, ( or added to the cron, if I get it to work). The below script works,... (5 Replies)
Discussion started by: tony3101
5 Replies

10. UNIX for Dummies Questions & Answers

While loops

Hi all, I am an amateur k shell scripter and came across something I need clarification on. while ; do various commands done What is the "" condition testing for in the above while loop? THx, Bookoo (5 Replies)
Discussion started by: bookoo
5 Replies
Login or Register to Ask a Question