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.
5) echo "Edit contact"
echo "Name of contact"
read r
grep -n "$r" contacts.txt
echo Number of contact
read n
for i in `grep -n "$r" contact.txt`
do
if [ -z $n ]
then
echo You did not typed anything
fi
done
echo "New name"
read var
echo "New surname"
read pav
echo "Number"
read tel
echo "Mail"
read el
sed -i "$n s/.*/$var $pav $tel $el/" contacts.txt ;;
Hello. This is part of my script. I want to make
Code:
if [ -z $n ]
then
echo You did not typed anything
That after this I back to main menu not exit. How to do that?
Please have a little patience. Don't bump your threads until at least a day has past. Not only is it frowned upon, but it takes your thread off the "zero reply" list, so you'll lose some visibility for it.
As I told you in your previous thread, don't act like you can demand help from us. You'll get a reply when someone has the knowledge, time, energy, and desire to do so. Try to work with the community, so we can learn together, ok?
As dive briefly mentioned, a common method is to set up your queries inside a continuous loop, and set the subcommands to break out of it only when you want it to. Here's a simple example:
Code:
#!/bin/bash
while true ; do
read -p "enter anything (or 'exit'): "
case $REPLY in
exit) break ;;
*) echo "You entered [$REPLY]" ;;
esac
done
Last edited by David the H.; 10-15-2011 at 09:44 AM.
Reason: minor correction
5) echo "Edit contact"
echo "Name of contact"
read r
grep -n "$r" contacts.txt
echo Number of contact
read n
for i in `grep -n "$r" contact.txt`
do
if [ -z $n ]
then
echo You did not typed anything
fi
done
echo "New name"
read var
echo "New surname"
read pav
echo "Number"
read tel
echo "Mail"
read el
sed -i "$n s/.*/$var $pav $tel $el/" contacts.txt ;;
Hello. This is part of my script. I want to make
Code:
if [ -z $n ]
then
echo You did not typed anything
That after this I back to main menu not exit. How to do that?
Look at the documentation on the break and continue keywords. You can specify the number of nested levels to break out of.
Edit:
And could you please take some time to format your code for better readability? Indent your loops and statements, and add some spacing lines. It's really hard to debug when everything is all lined up on the left side.
Edit2: Whoops, the formatting problem may be at least partially because you used quote tags, which don't protect whitespace. So please use [code][/code] tags around your code instead, which preserves all formatting.
Last edited by David the H.; 10-16-2011 at 12:37 AM.
Reason: addendum
After looking at the script a little more carefully (I had to copy it and reformat it to make it more readable), all you really need to do is set up a second sub-loop inside case 5), as it's as separate request from the main loop.
And to give you a few more suggestions for your code:
You can use an array to hold the menu entries and pass them to select. Example:
Code:
#!/bin/bash
en=( "Menu choice 1" "Menu choice 2" "Menu choice 3" "Exit" )
PS3="Enter the number of your selection: "
select choice in "${en[@]}" ; do
case $choice in
${en[0]}) echo "You chose: ${en[0]}"
;;
${en[1]}) echo "You chose: ${en[1]}"
;;
${en[2]}) echo "You chose: ${en[2]}"
;;
${en[3]}) echo "You choose to exit"
exit
;;
esac
done
2) I'd also set up each sub-command as a separate function first, then call them in the loop. Not only will it make the code more readable, but you could then later modify the function without worrying about the main code.
4) What the heck is the purpose for the blank read commands at the end of each entry? Did you include them in an attempt to move back to the main menu? If so, as shown, this will happen automatically as it finishes each sub-section, until you manually exit it.
What an ugly line. Do try to learn how quote marks are used. You only need one set for the entire string (not to mention that you forgot the final closing quote).
I mean really now, what is so hard about this? I gave you everything you need to know. Just alter the logic a bit and do exactly the same thing as in the main loop.
Ok, fine. Here.
Code:
5) while true ; do
echo "Edit contact"
read -p "Enter contact name" r
grep -n "$r" kontaktai.txt
read -p "Enter contact line number (or 'exit'): " n
case "$n" in
exit) echo "Operation cancelled."
break
;;
"") echo "No such number. Try again."
;;
*) grep -n "$r" contacts.txt
break
;;
esac
done
;;
It's untested though. I also have no idea if the grep commands do what you want, of course, since I have no idea what the file looks like or what actually you want to do.
Y'know, I personally learned how to script mostly by reading documentation, searching out solutions here and on the web, and just generally hacking away at problems until I find something that works. Usually I don't bother to ask for help until I've at least figured out some kind of a solution on my own.
I only rarely actually post questions here, and that's only when I'm really stumped, or when I want to confirm whether the solution I have is any good.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.