Need a help to move control to another line in my script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a help to move control to another line in my script
# 1  
Old 09-14-2011
Need a help to move control to another line in my script

I have menu option in my script and in that i have 4 options like below :-

Code:
echo "Please select a target server .."
         echo "[1] AIX - AIX1"
         echo "[2] AIX - AIX2"
         echo "[3] AIX - AIX3"
         echo "[4] HP-UX - HP-UX1"
 echo "Enter your menu choice [1-11]: \n"
         read tgtser
                case $tgtser in
                1|2|3|4)
                        echo "Checked out to test server ..." ;;
                *)echo "Opps !!! please select a valid option" ;;

If i gave anything other than 1 to 4, it is exiting now. But I want the control to be moved to first so that it will show the menu option again instead of exiting from the scriptSmilie...

Please help me out

Renjesh

Last edited by pludi; 09-14-2011 at 08:03 AM..
# 2  
Old 09-14-2011
Code:
#!/bin/bash

menu()
{
        clear
        echo "Please select a target server .."
        echo "[1] AIX - AIX1"
        echo "[2] AIX - AIX2"
        echo "[3] AIX - AIX3"
        echo "[4] HP-UX - HP-UX1"

        echo "Enter your menu choice [1-11]:"
}

menu

while read tgtser; do
case $tgtser in
        1|2|3|4)
                echo "Checked out to test server ..."
                break
                ;;
        *)
                echo "Opps !!! please select a valid option"
                sleep 2
                menu
                ;;
esac
done

exit 0

# 3  
Old 09-14-2011
Code:
#!/bin/bash

tgtser=""

function menu
{
        clear
                echo "Please select a target server .."
                echo "[1] AIX - AIX1"
                echo "[2] AIX - AIX2"
                echo "[3] AIX - AIX3"
                echo "[4] HP-UX - HP-UX1"
                echo "Enter your menu choice [1-11]: \n"
                read tgtser
}

while true
do
        menu
        case $tgtser in
            1|2|3|4)
               echo "Checked out to test server ..."
                break
                ;;
            *)echo "Opps !!! please select a valid option"
                sleep 2
                menu
                ;;
        esac
done

--ahamed

---------- Post updated at 04:20 AM ---------- Previous update was at 04:19 AM ----------

oh man!!... exactly (~99%) same code... even the sleep timer... Smilie

--ahamed
# 4  
Old 09-14-2011
Thank you for your help ...

I don't want to call another function for doing this ..
Is there something like "goto" command in which i can move the flow of the script ?
# 5  
Old 09-15-2011
@ahamed:
Smilie, I was thinking to have 3 seconds, but 3 was just too long - 2 was ideal watching the screen to change Smilie

@Renjesh:
Why not use functions? They are very helpful and help structuring your code and also prevent writing same code over and over.
# 6  
Old 09-15-2011
I will show the entire code and then probably you can help me ..
Code:
# Ver   Date            Description             Analyst
# ---   ----            -----------             -------
# A01  09/08/11      Original Implementation   Renjesh Raju
#***************************************************************************

function menu
{
    clear
    echo "[1] check-out to test server"
    echo "[2] check-in"
    echo "[3] check-out to local machine"
    echo "[4] Exit/stop"
    echo "----------------------------------------------"
    echo "Enter your menu choice [1-4]: \n"
    read choice
      case $choice in
      1)
         clear
         echo "Please select a target server .."
                echo "[1] AIX - AIX1"
                echo "[2] AIX - AIX2"
                echo "[3] AIX - AIX3"
                echo "[4] HP-UX - HP-UX1"
                echo "Enter your menu choice [1-4]: \n"
                read tgtser                
                case $tgtser in
                1|2|3|4|)
                        echo "Checked out to test server ..." ;;
                *)echo "Opps !!! please select a valid option" goto ;;
          (From here, i need to go to the sub menu again)
                esac
       ;;
      2) echo "Checked in ..." ;;
      3) echo "Checked out to local machine ..." ;;
      4) echo "Exiting ... " ;;
      *) echo "Opps !!! please select a valid option (1/2/3/4)" ;;
(From here, i need to go to the first menu again)
      esac
}
menu

It's a case looping .. Can you help me on this ?

thanks,
Renjesh

Moderator's Comments:
Mod Comment Use code tags, check your PMs and stop ignoring moderator instructions, thanks.

Last edited by zaxxon; 09-15-2011 at 07:47 AM.. Reason: code tags
# 7  
Old 09-15-2011
Moderator's Comments:
Mod Comment Stop ignoring PMs and instructions to use code tags - else you might be banned after collecting enough infraction points. You have more than 100 posts in this forum so you should know better.


Make separate functions for each menu and hand over parameters when calling the function. Example in theory:
  • When you select Checkin, assign a value to some variable like "ACTION=checkin" and call next menue with this parameter like menu2 $ACTION
  • In the second menu function process read the OS as you already do and do an action according to $ACTION, which will be $1 in this menu2().
  • When an actions follow up is to go back to some menu or the same, just call it again as part of a case/esac or if/then/fi.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Control M (^M) character with new line

Hi All, We are getting an external file in abc.csv format. When opened in excel spread sheet, it is opening alright. But when opened in notepad, I see the contents in a single line. Ftp'd the file in binary mode to AIX UNIX host. When opened, I see Control M (^M) characters in place of New Line... (16 Replies)
Discussion started by: njny
16 Replies

2. Shell Programming and Scripting

Move lines above a line onto one line

So say I have the file: john london 24 male ======== jane london 22 female ======== mike 23 ======== Bob how do i get the information i need on one line as such: (5 Replies)
Discussion started by: linuxkid
5 Replies

3. Shell Programming and Scripting

help: Awk to control number of characters per line

Hello all, I have the following problem: My input is two sorted files: file1 >1_19_130_F3 T01220131330230213311013000000110000 >1_23_69_F3 T01200211300200200010000001000000 >1_24_124_F3 T010203113002002111111200002010 file2 >1_19_130_F3 24 18 9 18 23 4 11 4 5 9 5 8 15 20 4 4 7 4... (9 Replies)
Discussion started by: DerSeb
9 Replies

4. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

5. UNIX for Dummies Questions & Answers

Inserting control characters at the end of each line

How to add control characters at the end of each line in a file? Can anyone help me with this? Thanks, Shobana (2 Replies)
Discussion started by: Shobana_s
2 Replies

6. Shell Programming and Scripting

move to a particular position and line

Hi, How can i move to particular line and to a particular position in a file using unix commands ? eg: in the line 30 and position 10 i want to print my name in a file. Cheers, Mohan (1 Reply)
Discussion started by: mohanpadamata
1 Replies

7. Shell Programming and Scripting

move to a particular line

Hi, I need to create a report in unix by moving to specified lines and specified positions and print some strings etc.. for eg: i need to print a string on 15th line and 7th position of a file. Please suggest. Thanks, Mohan (3 Replies)
Discussion started by: mohanpadamata
3 Replies

8. Shell Programming and Scripting

declaring array and move to next line in shell script

Hi, In shell script, I have a value and i like to move that value to a particular position in a file. For example, if i have 20000909 then i like to move that to the 15 to 23 position in a file. Is it possible to have array kind of thing in the shell ? a is array then a = 123 a =... (2 Replies)
Discussion started by: informsrini
2 Replies

9. Shell Programming and Scripting

Script to move the first line of a file to the end

I'm rather new to scripting, and despite my attempts at finding/writing a script to do what I need, I have not yet been successful. I have a file named "list.txt" of arbitrary length with contents in the following format: /home/user/Music/file1.mp3 /home/user/Music/file2.mp3... (21 Replies)
Discussion started by: Altay_H
21 Replies

10. Shell Programming and Scripting

curser control ~ multi line progress bar

Hi there, I have progress bars in several of my scripts, they work by deleting what has already been writen out, using: clear = "\\010"*( length of string i want to delete ) clearCmd = "echo -ne \""+clear+"\"" os.system(clearCmd) I then re-write the percent done & the progress... (4 Replies)
Discussion started by: purest
4 Replies
Login or Register to Ask a Question