how to access a variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to access a variable value
# 1  
Old 05-19-2008
Java how to access a variable value

hi
i have a function
abc()
{
a=1
b=2
c=3
}
Now i want to assign these values to the variables out side the function
k=a
l=b
m=c

please help
# 2  
Old 05-19-2008
Variables have global scope, unless explicitly declared local inside a function.

You need to interpolate the values that you assign, though.

Code:
k=$a
l=$b
m=$c

# 3  
Old 05-19-2008
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"
# 4  
Old 05-19-2008
Quote:
Originally Posted by aju_kup
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"
Included in a script and run, I get the following output
outside fn
outside fn
outside fn
# 5  
Old 05-19-2008
Works here (although I would perhaps have done it differently); which shell are you using?

Code:
bash$ cat jredx
#!/bin/sh
abc()
{
a=1
b=2
c=3
echo "k=$a \n l=$b \n m=$c"
}


eval $(abc)

echo "outside fn $k"
echo "outside fn $l"
echo "outside fn $m"

vnix$ ./jredx
outside fn 1
outside fn 2
outside fn 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Access another variable config variable

Hi all, Please help me on my problem. I want to take a value of variable which the variable is in other file. variable.sh wesly=1 wina=2 wekin=3 variabletaker.sh source variable.sh echo $wesly when i run the variabletaker.sh the error result is like here error (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

(solved) Shell scripting to access SQLPLUS using variable

I have shell script which will try to login to SQL Plus and retrieve some data, based on the outcome i will proceed further Below is the content of the file pebblz02% cat test1.ksh #! /bin/ksh dummyvar=`sqlplus -S csm_admin/csm_admin@SIDNAME <<EOF echo hi; exit; EOF` Error message on... (0 Replies)
Discussion started by: kiranlalka
0 Replies

3. Shell Programming and Scripting

export and access env variable in two scripts

Hi, I have two scripts say one.sh and two.sh. I want one.sh to continuously export a variable in loop. and when two.sh starts then it should read the last value exported from one.sh. file: one.sh #! bin/sh for i in `seq 1 1 4000000`; do export VAR=$(($i**$i)) ; done file two.sh ... (2 Replies)
Discussion started by: bhushan123
2 Replies

4. Shell Programming and Scripting

How to access variable from one file to another in shell script?

Hi, I had written shell script to access config file as input parameter. Contents of ConfFile.cfg are USER=ora PASSWORD=ora Shell script is DBConnect.ksh #!/bin/sh usage="`basename $0` <configuration_file_name>" DB_Connect() { sqlplus -s ora/ora << EOF CREATE TABLE TBL1... (10 Replies)
Discussion started by: Poonamol
10 Replies

5. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

6. Shell Programming and Scripting

Unable to access variable outside loop

I am unable to access the value set inside the loop from outside loop . Thought of taking this to forum , I had seen other replies also , where a pipe takes the execution to another shell and mentioned thats the reason we do not get the variable outside loop . But I am getting an issue and I am... (1 Reply)
Discussion started by: Armaan_S
1 Replies

7. Shell Programming and Scripting

unable to access a variable not local to a while loop

I have a while loop like this cat ${filename} | while read fileline do ... done I need to access a variable value $newfile inside this while loop How will i do that?? (6 Replies)
Discussion started by: codeman007
6 Replies

8. Programming

access variable through program stack

I am working on garbage collector in C? How should :confused: I find the part of heap where the variable are stored. It there any compiler (GCC) support for this. (2 Replies)
Discussion started by: amit gangarade
2 Replies

9. Shell Programming and Scripting

shell variable access

Hi I want to do the following: 1. Create a number of Gloabla varibale "ROUTE_IP_xx" based on a counter. As xx sould be from 1-10. ie ROUTE_IP_1 ROUTE_IP_2 . . ROUTE_IP_10 2. I want to initalize all of these variable to 0.0.0.0 ie ROUTE_IP_1='0.0.0.0' 3. I... (2 Replies)
Discussion started by: sabina
2 Replies

10. UNIX for Dummies Questions & Answers

Need help to access/mount so to access folder/files on a Remote System using Linux OS

Hi I need to access files from a specific folder of a Linux system from an another Linux System Remotely. I know how to, Export a folder on One SCO System & can access the same by using Import via., NFS in the Sco Unix SVR4 System using the scoadmin utility. Also, I know to use mount -t ... (2 Replies)
Discussion started by: S.Vishwanath
2 Replies
Login or Register to Ask a Question