hi guys, how would you do the following? I have a menu with 5 options in my shell script:
Run function 1 against files
Run function 2 against files
Run function 3 against files
Run function 4 against files
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?
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
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
# ./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 ....
# ./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