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? :wink:

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...

They can just run one after the other, it's just a read:

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 :confused:

Try something like:

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

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:

for i in $rSelection  (if there were a blank between the values) # but you could use while...
do
   <Your if paragraph
    fi >
done

That's a good point.

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

should do the things.

cheers guys, not really following though

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 :rolleyes:

love it! :smiley:

# ./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..
## 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 .... :wink:

# ./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

## 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

sweet :cool:

mad skillz guys, mad skillz

---------- Post updated at 03:10 PM ---------- Previous update was at 03:01 PM ----------

how about error checking this example so if the selection isn't 1 - 5 tell them to try again

It's already the case with ygemici's scripts (I think by the look of it...).
I would just add to ygemici's script using case: \n here:

   *) printf 'Invalid selection please try again \n' ;;

case construct is always a good choice for me also.

Moreover, if you are sure that functions are exists, you could also do something like:

for i in $choice
do
  type f${i} &>/dev/null && f${i} || echo "f${i} not found. Parhaps invalid choice?. Try again."
done