Set vars (by reference) in function


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Set vars (by reference) in function
# 1  
Old 06-12-2009
Set vars (by reference) in function

Hello everyone,
I am curious to find a possible way of doing something like this in ksh:
call a function and have that function set the value of the variable that the function knows by the name of $1....
example:

#! /bin/ksh
set_var(){
case $1 in
var1) this is where I would like to set var1 to a particular value;;
var2) this is where I would like to set var2 to a particular value;;
var3) this is where I would like to set var3 to a particular value;;
esac
}

set_var "var1"
set_var "var2"
set_var "var3"
# Now I would like to see what these values where set
echo $var1
echo $var2
echo $var3

Is this possible?
Hope you can help give me ideas.
Thanks.
# 2  
Old 06-12-2009
Code:
#!/bin/ksh
set_var()
{
case "$1" in
      "var1") var1="houston";;
      "var2") var2="boston";;
      "var3") var3="new york";;
esac
}

set_var "var1"
set_var "var2"
set_var "var3"
# Now I would like to see what these values where set
echo "${var1}"
echo "${var2}"
echo "${var3}"

# 3  
Old 06-12-2009
Well, what I would like to achieve inside the function is a statement somewhat like:

$1="houston"

inside my case statement, see, I do not want to have to explicitly key the var name since it has been passed to the function and it knows its name .....
# 4  
Old 06-13-2009
Is this what you're looking for?

Code:
[rick@kangaroo ~]$ cat kshtest.ksh 
#!/bin/ksh

export $1=$2

eval echo $1 = $(echo \$$1)
[rick@kangaroo ~]$ ./kshtest.ksh var1 houston
var1 = houston
[rick@kangaroo ~]$ ./kshtest.ksh var2 dallas
var2 = dallas
[rick@kangaroo ~]$

-----Post Update-----

Quote:
Originally Posted by gio001
Well, what I would like to achieve inside the function is a statement somewhat like:

$1="houston"

inside my case statement, see, I do not want to have to explicitly key the var name since it has been passed to the function and it knows its name .....
After reading this again, I'm confused. . .

Will var1, var2, etc. always be set to Houston? If so, then

Code:
export $1="Houston"

will work. If not, then the name of the variable must be in the case statement. Otherwise, how will the function know which value to assign? The explicit names of the variables will have to be typed as part of the case statement.
# 5  
Old 06-13-2009
Code:
#! /bin/ksh93

set_var()
{
   case $1 in
      var1)  eval $1=1 ;;
      var2)  eval $2=2 ;;
      var3)  eval $3=3 ;;
   esac
}

set_var "var1"
set_var "var2"
set_var "var3"

# Now I would like to see what these values where set
echo $var1
echo $var2
echo $var3

gives
Code:
1
2
3

# 6  
Old 06-13-2009
Is this only good for ksh93 or was it already ok for ksh88 ?
Thanks!
# 7  
Old 06-13-2009
Yes, will work with ksh88 also.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing vars or params to function

How to pass the getopts processed variable "${@}" to a function? It contains a list of package names needed in various functions. Seems the issue I have is due to the order of the script, with the processed "${@}" falling after the unprossed "${@}". I've been manually parsing options in the... (3 Replies)
Discussion started by: Cody Learner
3 Replies

2. Shell Programming and Scripting

set oracle variable in function

Hi, I have a function that is suposed to generate a AWR report: #-----------------------# gen_awr() #-----------------------# { sqlplus -s admin/admin@OCEAN11<<ENDOFSQL define num_days = '' define report_type = "html" define begin_snap =$snap1 define end_snap =%snap2 define... (1 Reply)
Discussion started by: amitlib
1 Replies

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

4. Shell Programming and Scripting

set -o noglob in a function

I don't understand noglob. I have a function: function no_globb () { set -o noglob echo "$1" set +o noglob echo "$@" } When I run the function like this noglobb *.txt The ouput is: file01.txt file01.txt file02.txt file03.txt Shouldn't it be *.txt file01.txt file02.txt file03.txt (1 Reply)
Discussion started by: AlphaLexman
1 Replies

5. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

6. UNIX for Dummies Questions & Answers

Object reference not set to an instance of an object

I am new to PHP and UNIX. I am using Apache to do my testing on a Windows Vista machine. I am getting this error when I am trying to connect to a web service. I did a search and did not see any posts that pertain to this. Here is my function: <?php function TRECSend($a, $b, $c, $d,... (0 Replies)
Discussion started by: EddiRae
0 Replies

7. Programming

undefined reference to 'function'

Hi, i want to compile a code where fortran calls c . First I compiled C module ,it has pass by gcc. However, after generating .o file. I compiled C and fortran files together by intel fortran and it failed " undefined reference to 'vicerror'". vicerror is a function. I do not know why. (1 Reply)
Discussion started by: austinhsu
1 Replies

8. Shell Programming and Scripting

Set lines of in a file to seperate vars

In a bash script, I'm looking for a way to set each matching line of a file into its own variable, or variable array. As an example, i have a crontab file with several entries: 00 23 * * * /usr/local/bin/msqlupdate -all 00 11 * * * /usr/local/bin/msqlupdate -inc 00 03 * * *... (2 Replies)
Discussion started by: lochraven
2 Replies

9. UNIX for Advanced & Expert Users

How to set a label/number for a function in a shells script

Can any one let me know like how cn i set an label or number for each function whihc are there in an shells script: for example cd /opt/qcom/test/ function1() function2() function3() ------- i ahe a script like this .now i want to count each of this function like 1, 2, 3,............... (3 Replies)
Discussion started by: lalitka
3 Replies

10. Shell Programming and Scripting

Keeping vars set in while loop with redirection

Under IRIX 6.5, the Bourne shell is named /bin/bsh. I need to redirect output into a file reading loop, and retain values set within the loop based on processing that goes on within the loop for later processing. This code #!/bin/bsh k=1 cat file | while read k ; do echo "$k" done... (2 Replies)
Discussion started by: criglerj
2 Replies
Login or Register to Ask a Question