Can a variable assigned in a shell function be accessed outside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can a variable assigned in a shell function be accessed outside
# 1  
Old 11-10-2009
Question Can a variable assigned in a shell function be accessed outside

Hi

I have the following script :
Code:
#!/bin/ksh
compare()
{
cat $1>t1
cat $2>t2
cy1=`cut -f13 -d'Ç' t1`
cy2=`cut -f13 -d'Ç' t2`
print "cy1 = $cy1"
print "cy2 = $cy2"
if [ $cy1 = $cy2 ]
then
  echo "yes"
else
  echo "no"
fi
}
 
d3=$(compare temp1.rm temp2.rm)
echo $cy1 <== Here the value assigned inside compare() should be displayed.

But I get blank value.
Any thoughts....Thanx in advance.. Am using SunSolaris, 5.8 and executing this in k-shell

Last edited by Franklin52; 11-10-2009 at 09:34 AM.. Reason: Please use code tags!
# 2  
Old 11-10-2009
Please show us your input files and display the value of the d variable.

Jean-Pierre.
# 3  
Old 11-10-2009
OK..Lemme be clear,
My script:
Code:
#!/bin/ksh
compare()
{
cat $1>t1
cat $2>t2
cy1=`cut -f13 -d'Ç' t1`
cy2=`cut -f13 -d'Ç' t2`
if [ $cy1 = $cy2 ]
then
  echo "yes"
else
  echo "no"
fi
}
d3=$(compare temp1.rm temp2.rm)
print $d3 <== Prints "yes" as cy1 and cy2 are equal.
echo $cy1 <== Is null.

I need to manipulate some part of the script using value of d3 and use value of cy1 down the script. How can I use the value for cy1 later??

Thx for the help !!!!

Last edited by Franklin52; 11-10-2009 at 09:35 AM.. Reason: Please indent your code and use code tags!
# 4  
Old 11-10-2009
Are you sure that cy1 and cy2 are not both null ?
In that case d3 will contains yes.

For debug purpose, modify your script :
Code:
compare()
{
   cy1=`cut -f13 -d'Ç' $t1`
   cy2=`cut -f13 -d'Ç' $t2`
   if [ $cy1 = $cy2 ]
   then
      echo "yes, values='$cy1/cy2'"
   else
      echo "no, values='$cy1/$cy2'"
   fi
}
d3=$(compare temp1.rm temp2.rm)
print "d3='$d3'"
print "values='$cy1/$cy2'"

You can use cy1, cy2 and d3 later in your script.


Jean-Pierre.
# 5  
Old 11-10-2009
Your problem is due to the fact that the function is executing in a subshell due to the use of $(...) syntax - not in the current process.

Here is a simplified example
Code:
#!/bin/ksh93

compare()
{
   var=3
}

d=$(compare)
echo "Var=${var}"

compare
echo "Var=${var}"

# 6  
Old 11-10-2009
Quote:
Originally Posted by fpmurphy
Your problem is due to the fact that the function is executing in a subshell due to the use of $(...) syntax - not in the current process.
Smilie Sometimes I must be blind.

Jean-Pierre/
# 7  
Old 11-11-2009
Thanx fpmurphy and aigles.. Got it worked..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl multiple qr assigned to variable

Experts, I'm having problems with the code below. I'm trying to test $var2 for two different regexs. I thought it could be done per below, but I'm getting the following error when running. $ ./test.pl b fed50c0100**** Unescaped left brace in regex is deprecated, passed through in regex; marked... (2 Replies)
Discussion started by: timj123
2 Replies

2. UNIX for Advanced & Expert Users

[SOLVED] Code does not run when assigned to a variable

I am more of a newbie, but wanted to post this in this forum as I was afraid no one would look at it in unix forums as it concerns shell scripting. I have a shell script that now runs fine with the exclusion of one line: x=`su nbadmin -c "ssh -t servery /usr/openv/netbackup/bin/bplist -C... (7 Replies)
Discussion started by: newbie2010
7 Replies

3. UNIX for Dummies Questions & Answers

How do you check if a variable has been assigned?

I am trying to check whether a variable has been assigned on the command line or not. Here is what I did: #!/usr/bin/bash if( $variable == '\0') { print "variable was not assigned" exit } else NF = 2 {print $1, ""} exit fi awk -f question1.awk variable = 58 letters.txt. So... (3 Replies)
Discussion started by: Fred63528
3 Replies

4. Shell Programming and Scripting

How to split a data assigned to a variable

The requirement is, there is a log file which contains a huge data. i need to get a particular field out of it by searching with another field. ex: 2011-03-28 13:00:07,423 : millis=231 q={ call get_data_account(?,?,?,?,?) }, params= i need to search for the word "get_data_account" in file... (1 Reply)
Discussion started by: Jassz
1 Replies

5. Shell Programming and Scripting

[Bash] Variable won't get assigned value

I am making of a script that will go through a couple of for loops and create file names based on the values in that loop, however the variable that combines everything is not getting assigned properly: #! /bin/bash for imod in K33_j1b_WS9_6 do for emod in mb2A mb2C mb3A mb3C mb4A... (1 Reply)
Discussion started by: badinsults
1 Replies

6. Shell Programming and Scripting

Missing Assigned Variable within logic operator

Hey , I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks while : do echo "what's ur name? (if none just press )" read name changeName = echo $name | sed "s/on/ey/" echo $changeName #this... (8 Replies)
Discussion started by: sexyTrojan
8 Replies

7. Shell Programming and Scripting

How to declare a variable which can be accessed globally

Hi I've few shell scripts which are responsible for triggering the continuous builds for a specific module. Each shell script is for a Module. Shell script has some module specific settings in the beginning and then it triggers the builds (which are nothing but some combination of Java programs... (2 Replies)
Discussion started by: kgsrinivas
2 Replies

8. Shell Programming and Scripting

Check if a variable has a value assigned?

Hi, I want to check if a variable has a value assigned to it or not. I can do following - cat $Var > File1 if then echo "$Var has value" else echo "$Var is null" fi But I have to check for 3 Variables and I want to wrap it up in couple of unix statements. Any... (3 Replies)
Discussion started by: sumeet
3 Replies

9. Shell Programming and Scripting

Return code of command assigned to variable

How do I evaluate the result of a command assigned to a variable?? Example: var1=`cmd` rc=$? rc will be the result of the assignment rather than cmd since it executes after. How do I evaluate the result of the command itself? Cheers..:confused: (2 Replies)
Discussion started by: browndr
2 Replies

10. UNIX for Dummies Questions & Answers

What does $? mean when assigned to a variable?

If i write this statement in a Korn Shell script RCODE=$? what possibly does it eman? (3 Replies)
Discussion started by: ranjita.c
3 Replies
Login or Register to Ask a Question