Shell script menu | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Shell script menu

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-24-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #2  
Old 09-24-2010
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
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...
Sponsored Links
    #3  
Old 09-24-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
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
    #4  
Old 09-24-2010
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 24 August 2014, 1:20 PM EDT
Location: Mumbai, India
Posts: 1,614
Thanks: 125
Thanked 184 Times in 178 Posts
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

The Following User Says Thank You to clx For This Useful Post:
rich@ardz (09-24-2010)
Sponsored Links
    #5  
Old 09-24-2010
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 2 September 2014, 1:19 PM EDT
Location: Switzerland - GE
Posts: 5,651
Thanks: 152
Thanked 391 Times in 370 Posts
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

The Following User Says Thank You to vbe For This Useful Post:
clx (09-24-2010)
Sponsored Links
    #6  
Old 09-24-2010
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 24 August 2014, 1:20 PM EDT
Location: Mumbai, India
Posts: 1,614
Thanks: 125
Thanked 184 Times in 178 Posts
That's a good point.


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

should do the things.
The Following User Says Thank You to clx For This Useful Post:
rich@ardz (09-24-2010)
Sponsored Links
    #7  
Old 09-24-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
cheers guys, not really following though


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

how does that work if I hit the 1 and 2 key for example?

---------- Post updated at 02:14 PM ---------- Previous update was at 02:02 PM ----------

sorry I see now

love it!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Write shell script using menu-driven approach to show various system bboyjervis SuSE 1 06-06-2010 11:01 PM
Menu Driven Shell Script which accepts1 to 5 options vaghya Homework & Coursework Questions 1 04-21-2010 07:19 AM
shell script to alter grub menu.lst ppucci Shell Programming and Scripting 1 06-27-2009 07:10 PM
Unix Shell Script: With Menu Option jroberson Shell Programming and Scripting 11 08-26-2008 06:04 PM
Changing korn shell script text Menu colors? darthur UNIX for Dummies Questions & Answers 6 01-20-2002 06:15 PM



All times are GMT -4. The time now is 03:02 AM.