Visit Our UNIX and Linux User Community


Reference Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reference Variable
# 1  
Old 10-01-2007
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  
Old 10-01-2007
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  
Old 10-01-2007
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  
Old 10-01-2007
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  
Old 10-01-2007
Yes, it does! That makes more sense as it's apparent I'm no scripting expert.

Thank you both for your replies!
# 6  
Old 10-02-2007
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  
Old 10-02-2007
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #498
Difficulty: Medium
Learning a computer programming language is harder than learning a new spoken language.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get last reference date

Hi, Could you please help me to get last reference date in Unix, in Unix we maintain SAS7BDAT files. Is there any command or script to get the info, Thank you. (2 Replies)
Discussion started by: subbarao12
2 Replies

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

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

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

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

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

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

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

9. HP-UX

Reference Material

I'm having to write 'C' programs for a group of HP-UX 11.X systems. While I have found many 'C' reference books which I like, none of them reference using 'C' in the UNIX environment. Anyone have any good recommendations for books about 'C' programming under UNIX? Thanks. Chris (0 Replies)
Discussion started by: chrisc@nwark.ne
0 Replies

Featured Tech Videos