LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-11-2007, 08:53 PM   #1
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Rep: Reputation: 0
Help with a basic script - adding a menu selection


I have a script that I would like to add a sub menu to.

This means when the user chooses 1 from this menu (see original part of script):
echo "[1] Box 110"
echo "[2] Box 110H"
echo "[3] Box 120"

They receive a subset of options such as:

(a) Tommy
(b) David
(c) Mike

So what I'm really looking to do is first press 1, 2, 3, then if a user presses 1, then they receive the subset of choices (a, b, and c). Then I can do my copying, symbolic link, etc on option a, b, or c.

Hopefully this is not too confusing.

#!/bin/bash
# Modified 2007/03/06

clear # Clear the screen.

echo " Box configurations"
echo " ------------------"
echo "Choose product type:"
echo
echo "[1] NET 110"
echo "[2] NET 110H"
echo "[3] NET 120"
read PRODUCT
echo


# Delete old software files
rm -Rf /tftpboot/*


if [ "$PRODUCT" -eq a ]; then
cd /usr/local/images/A110
rm current
ln -s xxx current

echo ""
echo "System is now configured for upgrading and converting to NET 120"
exit

elif [ "$PRODUCT" -eq b ]; then
cd /usr/local/images/A120
rm current
ln -s xxx current

echo ""
echo "System is now configured for upgrading and converting to NET 110"
exit

Thanks, Owen
 
Old 03-11-2007, 09:02 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
What is your specific question? What have you tried that hasn't worked quite right?
 
Old 03-11-2007, 09:19 PM   #3
2damncommon
Senior Member
 
Registered: Feb 2003
Location: Calif, USA
Distribution: PCLINUXOS
Posts: 2,918

Rep: Reputation: 103Reputation: 103
"case" is good for menus.

EDIT: OK, here is an example.

Last edited by 2damncommon; 03-11-2007 at 09:28 PM.
 
Old 03-11-2007, 09:22 PM   #4
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Original Poster
Rep: Reputation: 0
Hello, basically I'm not sure how to write a script that when I first run the script, it give me 3 choices, i.e.:

Choose Configuration
[1] Box 110
[2] Box 110H
[3] Box 120

THEN... if I choose option [2], it give me a brand new screen to choose between say 2 option, i.e.:

(a) Install version 0.12.3
(b) Install version 0.14.0

So, once I choose option b, then I can do my copying, symbolic linking etc... I have no problem setting it to the the linking and copying on the second set of sub options, but I need to know how to set up the menu so that it gives the original 3 choices, but then give me a sub set of choices, and then I can act on the sub set.

Do that make sense?

thx, Owen
 
Old 03-11-2007, 10:31 PM   #5
Guitarist88
Member
 
Registered: Feb 2004
Location: Nz
Posts: 240

Rep: Reputation: 30
You use a switch statement. And have the user give a choice of what they want and go to something else according to their choice... here is an example from one of my scripts:

cat <<- _eof_
####### Main Menu #######
1) Add Installation Sources
2) Add Windows Media, MP3, and DVD playback support
3) Add propriety packages (flash, java, etc.)
4) Video card drives (Nvidia Only)
5) 3D desktop effects
6) Create Beryl autostart script
7) Exit

_eof_

echo -n "Selection: ";read choice
case $choice in
1 ) repo ;;
2 ) media ;;
3 ) propriety ;;
4 ) video ;;
5 ) beryl ;;
6 ) autostart ;;
7 ) clear; exit 0 ;;
* ) echo "Please make a valid choice"; exit 1;;
esac

Each one of those goes to a different function. That is how a switch case works though. If you need any other help... let me know.

Last edited by Guitarist88; 03-11-2007 at 10:35 PM.
 
Old 03-12-2007, 03:43 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
there is a built in for doing multi choice
menus, select

example:
select f in * ;do echo $f;done
 
Old 03-12-2007, 04:25 AM   #7
spaesani
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Rep: Reputation: 0
there is also:


selection={"selection msg" selection1 selection2 selection3 selection4 ... selectionN}
sellen=6
# if you want to get fancy, use tput to position your menu
tput init
tput 20 20

while [ 1 ]; do

# this is to leave a line above the selection msg for clarity:
echo
# selection[0] is the message for the selection:
echo ${selection[0]}
# selections start at 1
let i=1
while [ $i -lt sellen ]; do
# here you can format your selections as you like
# either [$i] or ($i) or $i- etc...
echo [$i] ${selection[$i]}
let i=$i+1
done
# here read in only one character using the -s option of read
read -s 1 k
# key entry handler:
case $k in
1|2|3|4|5|6)
# if selection is a msg:
echo ${selection[$k]}
# if selection is a command or function name simply call it:
${selection[$k]}
break
;;
*)
# if you want a clear screen do so here:
clear
# and if you want to re position the menu
tput 20 20
;;
esac
done

# try keying in something other than 1,2,3,4,5,6
# the message will reappear in the manner it was formatted
# personally i prefer this method than using select

cheers
sp
 
Old 03-12-2007, 04:45 PM   #8
okorkie
Member
 
Registered: Mar 2007
Posts: 47

Original Poster
Rep: Reputation: 0
Problem solved!!

Thanks so much for everyone's help.

-Owen
 
  


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
Shell script adding autostart gnome script Coolrunr Programming 3 01-01-2009 02:23 PM
XFCE Basic Menu Editing Question binarybob0001 Linux - Software 4 05-29-2006 05:22 PM
Some basic advice for hardware selection please plastic_gnome Linux - Wireless Networking 1 02-13-2005 02:58 PM
Basic Question about update-menu -9 command Rjones1981 Linux - Newbie 0 03-10-2004 08:20 AM
Adding a KDE menu to gnome menu Projekt2 Slackware 3 10-25-2003 01:47 PM

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

All times are GMT -5. The time now is 12:10 AM.

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