Register
It is currently Fri Nov 28, 2014 7:09 am

help with menu in bash using case


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Tue Aug 23, 2011 9:27 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Hello,
I am creating (or trying to) a simple script that will set the parameters of eth0 or eth1 depending on what the user inputs. I can do this without an issue, but my problem is if the user says 'no' upon confirmation that they are selecting the correct NIC, I need a way to "loop" it back to the start of the question (meaning they made a typo or something). I This will be in a kickstart script, so simply killing the script and restarting it isn't an option.

Here is what I have so far:

Code:
#!/bin/bash

cat << MENU
Select which NIC to configure:
1 - eth0
2 - eth1
MENU

read NIC

case "$NIC" in

"1" )
echo -e "Are you sure you want to configure eth0? (y/n)? \c"
read answer
  if [ "$answer" = y ] ; then
    echo "Fill in the following questions:"
    #blah blah blah...I'll grab their responses and input them accordingly into ifcfg-eth0
  else
    echo "Let's start over."
    #this is where I need to redirect it back to the first question
  fi
;;

"2" )
echo -e "Are you sure you want to configure eth1? (y/n)? \c"
read answer
  if [ "$answer" = y ] ; then
    echo "Fill in the following questions:"
    #blah blah blah...I'll grab their responses and input them accordingly into ifcfg-eth1
  else
    echo "Let's start over."
    #this is where I need to redirect it back to the first question
  fi
;;

esac

exit


thanks!


Top
 Profile  
 PostPosted: Tue Aug 23, 2011 2:00 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
basic syntax is this
Code:
#!/bin/bash -e

flag=1
while (($flag))
   do
      read x
      echo $x
      if [ "$x" = "quit" ];then
         flag=0
      fi
   done

it will keep looping untill the user enters "quit"


Top
 Profile WWW  
 PostPosted: Wed Aug 24, 2011 2:22 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Nice solution!

Here's another way to do it with minimal modification of your existing code! I like to use functions, especially if what I'm working with is part of a larger script. In this case, you might be able to put your menu prompt bit into a function, and have it call the function again if the user answers "n"

(Lines I've added are marked with "##")
Code:
#!/bin/bash

## Let's enclose the menu prompt in a function!
PROMPT () {  ##This line begins the function
cat << MENU
Select which NIC to configure:
1 - eth0
2 - eth1
MENU

read NIC
}  ## This bracket closes the function


## Here's where we run the function the first time
PROMPT

## Now we do a loop, similar to Keith's while loop
until [ "$answer" = "y" ]; do

  case "$NIC" in

    "1" )
    echo -e "Are you sure you want to configure eth0? (y/n)? \c "
    read answer
    if [ "$answer" = y ] ; then
      echo "Fill in the following questions:"
      #blah blah blah...I'll grab their responses and input them accordingly into ifcfg-eth0
    else
      echo "Let's start over."
      #this is where I need to redirect it back to the first question
      PROMPT  ## This displays the menu again
    fi
    ;;

    "2" )
    echo -e "Are you sure you want to configure eth1? (y/n)? \c "
    read answer
    if [ "$answer" = y ] ; then
      echo "Fill in the following questions:"
      #blah blah blah...I'll grab their responses and input them accordingly into ifcfg-eth1
    else
      echo "Let's start over."
      #this is where I need to redirect it back to the first question
      PROMPT  ## This displays the menu again
    fi
    ;;

  esac
done  ## This ends the 'until' loop

exit


Depending on the version of bash you're using, it might like the function to start with the word "function", but this works in my shell, and seems to be the closest to a "drop-in" solution for your existing script.

There are more ways to streamline this script too! For instance, your verification y/n code could be put into a function, so that you don't have essentially the same 'if' statement in there twice.

Also, you can replace these two lines:
Code:
echo -e "Are you sure you want to configure eth1? (y/n)? \c"
read answer

...with this ONE line:
Code:
read -p "Are you sure you want to configure eth${NIC}? (y/n)? " answer


I hope this helps!
( Sorry for the unsolicited extra bits :$ )


Top
 Profile YIM  
 PostPosted: Fri Aug 26, 2011 1:39 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Thanks keith and jeo... that info worked very nicely.


Top
 Profile  
 PostPosted: Sat Sep 10, 2011 11:15 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
normally for a menu, i create a function that looks and goes something like this.....
Code:
menu ()
{
echo "     1)     Do thing #1"
echo "     2)     Do thing #"
echo "     3)     Do thing #"
echo "     4)     Do thing #"
echo "     5)     Do thing #"
echo "     6)     Do thing #"
echo "                     ---------"
echo "     0)     Exit"
echo "                     ---------"
echo " ";
echo -n "     Please choose one of the options: " ; read option
   case $option in
      "1")   function_one; # you can call other functions here
         ;;
      "2")   ls -lah; # you can run a command here
         ;;
      "3")   echo "hello"; # you can print something if you want
         ;;
      "4")   function_four;
         ;;
      "5")   function five; # normally I call addition functions from the case statement
         ;;
      "6")   function_six;
         ;;
      "0")   exit 0;  #this exits
         ;;
        "")     menu ;
         ;;
      *)   echo "     That choice was invalid!! Hit any key to try again";read temp;menu
         ;;
   esac
}

menu; # calling the above function



That's how I do all of my menu's, and have done some rather large ones that way :)
case/esac also works great if you need a ton of if/fi statements.

Hope that's useful for someone.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP