Shell script menu


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script menu
# 1  
Old 09-24-2010
Shell script menu

hi guys, how would you do the following? I have a menu with 5 options in my shell script:

1. Run function 1 against files
2. Run function 2 against files
3. Run function 3 against files
4. Run function 4 against files
5. Run function 5 against files

I'd like to be able to run multiple functions however based on what the user inputs, for for example if I press key 1,2 and 4 then I want to run function 1, 2 and 4, if I press key 1 and 4 then only run function 1 and 4, if i just press key number 1 then i only want to run function 1 - any ideas? Smilie
# 2  
Old 09-24-2010
Depends on how you wrote your menu script:
How do you capture the input? in sequence on in one go?

What are these functions? should they be running at the same time or one after the other...
# 3  
Old 09-24-2010
They can just run one after the other, it's just a read:

Code:
read rSelection
if [[ $rSelection == '1' ]] then
    #call function 1
    else
    if [[ $rSelection == '2' ]] then
    #call function 2
    else
    if [[ $rSelection == '3' ]] then
    #call function 3
    else
    if [[ $rSelection == '4' ]] then
    #call function 4
    else
    if [[ $rSelection == '5' ]] then
    #call function 5
    else
    echo "Exiting"
    exit
    fi
fi

That's what i have at the moment, too basic for what i need Smilie
# 4  
Old 09-24-2010
Try something like:


Code:
f1 () { 
echo running fn1 
}
 
f2 () {
echo running fn2 
}
 
f3 () { 
echo running fn3 
}
 
f4 () { 
echo running fn4 
}
 
echo "enter choice -- multiple choice can be provided separated by space"
 
read choice
 
set -- $choice
 
for i in $@
do
 echo "run function $i"
 f${i}
done

This User Gave Thanks to clx For This Post:
# 5  
Old 09-24-2010
What I see (but its friday...) is ONE read
so all your values (or just one) are read in one go.
One way to do things would be to stripe the entry ( what do you see when you enter 123? do you get 1 2 3 or 123? in other words is there a separator?) and use the values in a loop
That would give you something like:
Code:
for i in $rSelection  (if there were a blank between the values) # but you could use while...
do
   <Your if paragraph
    fi >
done

This User Gave Thanks to vbe For This Post:
# 6  
Old 09-24-2010
That's a good point.

Code:
for i in $choice
do
 echo "run function $i"
 f${i}
done

should do the things.
This User Gave Thanks to clx For This Post:
# 7  
Old 09-24-2010
Code:
# ./justdoit
1) one
2) two
3) three
4) four
5) five
6) quit
Please input your choice..1

This is f1 function

1) one
2) two
3) three
4) four
5) five
6) quit
Please input your choice..

Code:
## justdoit ##
#!/bin/bash

f1 ()  {
echo -e "This is f1 function\n"
}

f2 ()  {
echo "This is f2 function"
}

f3 ()  {
echo "This is f3 function"
}

f4 ()  {
echo "This is f4 function"
}

f5 ()  {
echo "This is f5 function"
}

while [ "$i" != "6" ];   do
echo "1) one
2) two
3) three
4) four
5) five
6) quit"
read -p "Please input your choice.." i
echo ""

case $i in
   1) func=1 ;;
   2) func=2 ;;
   3) func=3 ;;
   4) func=4 ;;
   5) func=5 ;;
   6) break ;;
   *) printf 'Invalid selection please try again' ;;
  esac

for i in 1 2 3 4 5
 do
   if [ "$func" == "$i" ]; then
   f$i
   fi
 done

done



---------- Post updated at 04:44 PM ---------- Previous update was at 04:26 PM ----------



And one more value version like 1232 2323 124 .... Smilie

Code:
# ./justdoit
1) one
2) two
3) three
4) four
5) five
6) quit
Please input your choice..124

This is f1 function
This is f2 function
This is f4 function
1) one
2) two
3) three
4) four
5) five
6) quit

Code:
## justdoit ##
#!/bin/bash

f1 ()  {
echo "This is f1 function"
}

f2 ()  {
echo "This is f2 function"
}

f3 ()  {
echo "This is f3 function"
}

f4 ()  {
echo "This is f4 function"
}

f5 ()  {
echo "This is f5 function"
}

while [ "$i" != "6" ];   do
echo "1) one
2) two
3) three
4) four
5) five
6) quit"
read -p "Please input your choice.." i
echo ""

for x in $(echo `echo $i | fold -w1`)
 do
   f${x}
 done

done

This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to include menu based options in Shell script?

Hi Friends, I have a menu based tool which requires input/option to proceed further. How to make a shell script ? eg: menu looks like Get_data.sh to continue (y/n) : Here I need to key in "y" to proceed. I want to prepare a script which should consider option y. (5 Replies)
Discussion started by: suresh3566
5 Replies

2. Shell Programming and Scripting

Automate the menu options using shell script

I have a menu option which will look as follows Select a menu option 1.change password 2.login as root user 3.show system version 4.quit Select> 1 please enter the new password: unix reenter the new password: unix press any key to enter (then displays again the menu options to enter the... (4 Replies)
Discussion started by: shivakumar6g
4 Replies

3. Shell Programming and Scripting

Menu shell script help

Hi All, I have written a shell script that show menu driven option. My requirement is that in the menu driven option i want to select multiple choice. i.e if i want to select 1 or 1,2 or 1,2,3 or 2,3 etc .... Can some one help me in that My script. while true; do echo " " ... (8 Replies)
Discussion started by: Nawrajesh
8 Replies

4. Shell Programming and Scripting

A selection menu in a shell script

I'm writing a shell script and have a problem with selection when I issue the command, is there a way to automatically choose a selection number one after a selection menue appear Command 1-choice 2- choice 3-choice Thanks Sara (3 Replies)
Discussion started by: Sara_84
3 Replies

5. Shell Programming and Scripting

Shell script menu problem

I have tried searching the forum but i haven't found a solution for this. I have a shell script that presents the users with menus. The menus branch out to sub menus. It is all hunky dory as long as i traverse forward. But if i am in a sub menu and return to the previous menu and choose any... (11 Replies)
Discussion started by: goddevil
11 Replies

6. Shell Programming and Scripting

Menu using shell script

Hi, I need to have a shell script for the below need. 1. Menu with one heading and 4 options. 2. the heading and 4 options are taken from a file. File entry ====== Heading1|option1|option2|option3|option4| Heading2|option1|option2|option3|option4| 3. the user entries must be captured in... (9 Replies)
Discussion started by: umastinu
9 Replies

7. Homework & Coursework Questions

Menu Driven Shell Script which accepts1 to 5 options

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) Write a Menu Driven Shell Script which accepts1 to 5 options and performs the following actions for... (1 Reply)
Discussion started by: vaghya
1 Replies

8. Shell Programming and Scripting

shell script to alter grub menu.lst

Hi folks, I have a dual-boot Ubuntu/Windows machine and I wanted to create a script to change the menu.lst file so it will change the default boot partition (this is so I can reload the machine remotely and allow it to boot to the Windows partition). Today I have to sudo cp a template file I... (1 Reply)
Discussion started by: ppucci
1 Replies

9. Shell Programming and Scripting

Unix Shell Script: With Menu Option

I am attempting to create a shell script with the following capaciblities: 1. Listed options to choice from 2. Use to perform awk statements 3. Print a report with the awk results My questions are 1. How do I select more than one file for option #5 and #6 2. How to I create an... (11 Replies)
Discussion started by: jroberson
11 Replies

10. UNIX for Dummies Questions & Answers

Changing korn shell script text Menu colors?

Is it possible to change the color of text in a korn shell script Menu? I can change the color of session text through my telnet client but I want to be able to change color text in the Korn shell menu to highlight certain items. (6 Replies)
Discussion started by: darthur
6 Replies
Login or Register to Ask a Question