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 10-15-2011, 01:50 AM   #1
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Rep: Reputation: Disabled
bash back to main menu


Code:
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?

Last edited by trintukaz; 10-15-2011 at 04:37 AM.
 
Old 10-15-2011, 04:38 AM   #2
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Anyone?
 
0 members found this post helpful.
Old 10-15-2011, 05:08 AM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Probably the best way is to make functions and have it loop.
 
Old 10-15-2011, 09:43 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 10-15-2011, 03:43 PM   #5
linuxxer
Member
 
Registered: Apr 2011
Location: I have my own planet
Distribution: SlackwareŽ
Posts: 257

Rep: Reputation: Disabled
Quote:
Originally Posted by trintukaz View Post
Code:
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?
Hi,

I like to correct something.

if [ -z "$n" ]
 
Old 10-16-2011, 12:12 AM   #6
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
...

Last edited by trintukaz; 10-16-2011 at 06:32 AM.
 
Old 10-16-2011, 12:28 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

http://mywiki.wooledge.org/BashGuide...es#Readability

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
 
Old 10-16-2011, 01:14 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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:

1) You might try using a select menu for the main menu instead of building your own. It automatically loops. http://www.tldp.org/LDP/abs/html/testbranch.html#EX31

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.

3) $(..) is highly recommended over `..`. Please don't use backticks, there's no reason for them, and many reasons against.

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.

5)
Code:
echo "$var," "$pav," "$tel"," "$el >> contacts.txt;;
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).
Code:
echo "$var, $pav, $tel, $el" >> contacts.txt ;;
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also see the best practices page I pasted above.

Last edited by David the H.; 10-16-2011 at 01:16 AM. Reason: format fix
 
Old 10-16-2011, 01:45 AM   #9
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
I don't know maybe I am asking to much but can you shows how to make that sub loop for 5)?
 
Old 10-16-2011, 05:14 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

You might want to give it a try yourself.
 
1 members found this post helpful.
Old 10-16-2011, 06:28 AM   #11
trintukaz
Member
 
Registered: Sep 2011
Posts: 82

Original Poster
Rep: Reputation: Disabled
Nice! It works fine now thank you very much now finally I can finish my script.
 
  


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
application menu disappeared and the main menu item has no response sunnior Ubuntu 12 11-03-2011 04:28 AM
some icon file disappeared from the place menu in the main menu of ubuntu 11.04 how t shevos37 Linux - Newbie 1 10-10-2011 02:59 PM
How to change GNOME main menu icon(Applications menu) deepclutch Linux - Software 7 06-25-2007 01:31 PM
gnome main menu behaviour (menu follows mouse) metalloid Linux - Software 2 06-29-2006 01:58 AM
Problem with pkgtool. Falls back to main menu PurpleMotion Slackware 5 05-12-2004 09:48 AM

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

All times are GMT -5. The time now is 01:52 PM.

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