Reference Variable


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Question 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!
# 2  
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
# 3  
Hmm...not sure I know what to do with that Smilie Any more details? I can't say I've ever done anything like that before...
# 4  
Is this easier?

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

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

Thank you both for your replies!
# 6  
Error Confusion...

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?
# 7  
Code:
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.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #915
Difficulty: Easy
In Javascript, 'UNIX.com'.slice(); returns UNIX.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

2. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

3. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i know when undefined reference shows up the program is saying it is not link to that function but the problem... (1 Reply)
Discussion started by: mgyeah
1 Replies

4. Shell Programming and Scripting

Symbol reference

Hi, test -d .ssh || mkdir .ssh && chmod 700 .ssh The command has couple of symbols, could someone redirect me to the link, where i can understand their significance. Thanks, John (1 Reply)
Discussion started by: john_prince
1 Replies

5. Shell Programming and Scripting

BASH - Reference external variable name dynamically

Hi there, I have included an external properties file into my BASH script via the 'source' command. I am attempting to dynamically assign a variable in the BASH script, that references the variable name within the external properties file i.e. #!/bin/bash pth=${0%/*} source... (3 Replies)
Discussion started by: mjwoodford
3 Replies

6. Shell Programming and Scripting

Perl reference

Hi all, I have a reference named $test. it points to the data structure as follows 'test' => }, ... (1 Reply)
Discussion started by: Damon sine
1 Replies

7. Shell Programming and Scripting

subsequently reference variable

Hello, This is not homework. It is a question that I received on a recent interview for a linux position. Can someone shed some light on the right answer? I got it wrong. Thanks, jaysunn (3 Replies)
Discussion started by: jaysunn
3 Replies

8. Shell Programming and Scripting

Unix Variable Reference and Substitution

I can't seem to make what appears to be a simple substitution. I want to define a list of systems for which daily reports need to be filed systems="systemA systemC systemZ" I then want to run a loop for i in ${systems} Analyze statistics Create the reports mailx (8 Replies)
Discussion started by: mugsymark
8 Replies

9. Shell Programming and Scripting

How to reference a variable within sed?

Hi all, How can I use sed to perform a substitution if the string that I'm going to substitute is stored in a variable: Let's say: sed 's/abcdefg/good' VS tmp="abcdefg" sed 's/$tmp/good' The second case doesn't work. Guess it's due to the single quotes on the outside. How can I... (1 Reply)
Discussion started by: rockysfr
1 Replies

Featured Tech Videos