I need to determin the most efficient way to do something (rather simple, I thought).
I'm currently echo(ing) a series of menu options, and reading the command input as the number associated with the entry. What I need to do is when the option 1 is selected, that it references a list and then fills in the variable the correct way.
Example - if I have a friendly list of server names like:
Server 1
Server 2
(...and so on)
However, in the command that I am envoking in the background it needs to be ipaddress : port. I don't want the IP address as the name on the menu, but it does need to be passed for the process to work correctly.
I would have a function that uses a case statement, it takes the option argument and returns the various associated elements with that option in a strict order....
PS3="choose:"
select server in server1 server2;do
case $server in
server1)printf "You choose $REPLY : use your ip_1:port here\n";;
server2)printf "You choose $REPLY : use your ip_2:port here\n";;
*)printf "Invalid option\n";break;;
esac
done
I was using radoulov's example and had gotten much further.
I see two things happening that I need to change.
(1) When I select an option from the list, it simply outputs the $REPLY value back to the console. The user doesnt need to see this value, but instead it needs to be passed as a variable to the command to be executed.
(2) Once I select an option from the list, it just keeps prompting me for another option to select and doesn't move on to complete the script. Am I missing an end, or done, or something?
select server in server1 server2;do
case $server in
server1) <use your ip_1:port here>;break;;
server2) <use your ip_2:port here>;break;;
*)printf "Invalid option\n";break;;
esac
done
... continue with your code here.
You need to substitute <use your ip_1/2 port here> with your code.
Use continue instead of break if you want to offer another choice.
I did the substitution, and it is returning the correct value.
I have break at the end becuase I only want the user to be able to select ONE of the options. Once one is selected it then ends with the value and moves on to the next step.
All I need to have happen is when the user selects #1 and presses enter, then the value of what 1 corresponds to in the background gets passed to a -switch on the final command in the script.
Example:
Main Menu:
1 - Red
2 - Green
3 - White
When the user selects option 2 - green, I need the script to pass a reference value of what the user selects to an actual value that the command can interperet.
So in this example, instead of having the user know the color code of green in html (ie. #123456) he can just select green, but the script will pass #123456 to the actual switch in the command to be executed.
zsh 4.3.4% cat script
#!/bin/zsh
select color in Red Green White;do
case $color in
Red)code='#FF0000';break;;
Green)code='#00FF00';break;;
White)code='#FFFFFF';break;;
esac
done
print -l "You choose: $color, the code is $code"
zsh 4.3.4% ./script
1) Red 2) Green 3) White
?# 2
You choose: Green, the code is #00FF00
Now I see what you are thinking, and it's not what I am thiniking. Let me try to clarify:
The user does not need to know the translated value.
The translated value needs to be passed into a command line -switch.
For example, if the user selects 2 - green, then I want the #123456 value to held in memory as a variable that I can reference when the final command is issued.
so, to clarify, put the example you supplied into the suggested format:
select server in server1 server2;do
case $server in
server1) ipaddy="192.168.0.4:21";break;;
server2) ipaddy="192.168.0.5:990";break;;
*)printf "Invalid option\n";break;;
esac
done
ftp username@"$ipaddy"
If the user chose server1 the ftp session is subsequently established with 192.168.0.4 on port 21.
If they chose server2 the ftp session is with 192.168.0.5 on port 990.