ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
#!/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
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?
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.