Reference Variable

Hi!

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:

  1. Server 1
  2. 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.

How would I do this?

Thanks!

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

get_menu()
{
case "$1" in
1 )
echo "option one"
echo "flim"
echo "flam"
;;
2 )
echo "option two"
echo "foo"
echo "bar"
* )
return 1;
esac
}

then you could do

get_menu 1 | while read N ....

then you have also centralised all the behaviour of the menu

Hmm...not sure I know what to do with that :eek: Any more details? I can't say I've ever done anything like that before...

Is this easier?

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

Yes, it does! That makes more sense as it's apparent I'm no scripting expert.

Thank you both for your replies!

Ok guys, I think I'm missing something here.

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.

Does that make any more sense?

Ok!

I have the break working properly.

Now all I need is to have the $REPLY value passed to the command line.

Is the $REPLY setting a variable that can be used in the rest of the script or am I chasing my tail?

Hm, IMHO you don't need the $REPLY,
you just need to use the proper IP when server1 or server2 is selected ...
Am I missing something?

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.

Any better?

And what makes you think you can't use the variable code as -switch without outputing it?

 ... -switch "$code" 

insted of:

print -l "You choose: $color, the code is $code" 

shell does not need a break statement like C or C++. With a double ";;" it does not fall into the next case.

... but you need it to exit the select loop:

zsh 4.3.4% select v in 1 2 3;do
select> case $v in
select case> 1) print $REPLY;;
select case> 2)print $REPLY;; 
select case> esac
select> done
1) 1  2) 2  3) 3  
?# 1
1
?# 2
2
?# 3
?# 
zsh 4.3.4% select v in 1 2 3;do
case $v in
1) print $REPLY;break;;
2)print $REPLY;;
esac
done
1) 1  2) 2  3) 3  
?# 1
1
zsh 4.3.4% 

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.

Capische? :slight_smile:

Capisce (not capische) chi?

Thank you for teaching me how to explain ... :slight_smile:

The script is working brilliantly!

Thanks everyone for your time and input!