Variable value not retaining outside function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable value not retaining outside function
# 1  
Old 09-02-2010
Variable value not retaining outside function

Hi All,
As per my understanding, value of variable is retained outside function.
But the value of array myarrayDriver[$i] is not retained outside function. Could you please tell the reason for the same..

code:
Code:
readingConfigFile()
{

search_keyword="$1"
i=0
for  pointer in $(cat wrapper_script.config)
do
        if [ "$search_keyword" == "drivers=" ]
        then
                # The below line does not execute when in the function body
                # See the next un-commented line which works instead
                # var=`echo $pointer |awk -F= '/'"$search_keyword"'/ {print $2}'`
                var=$(echo $pointer |awk -F= '/'"$search_keyword"'/ {print $2}')
                if [ "$var" != "" ] && [ "$search_keyword" == "drivers=" ]
                        then
                        myarrayDriver[$i]=$var
                        i=$(expr $i + 1)
bla .. bla...
}
 echo "${myarrayDriver[0]} "  #not returning value

# 2  
Old 09-02-2010
There are two things:

1. Make sure this function gets called.
2. If it is called, make sure that the array element has actually some value. You can put a set -x inside the function to see what is happening in detail.
# 3  
Old 09-02-2010
Stupid question, but where do you call the function in your script?
In the statement
Code:
if [ "$var" != "" ] # && [ "$search_keyword" == "drivers=" ] # not necessary

You can remove the second test, because you are already in an if statement for that condition (it should always be true then). You can also write it
Code:
if [ -n "$var" ] # string contains something

If you really call the function, an echo "$var" should give you something
# 4  
Old 09-02-2010
klaxxon,

1. I tried set -x, and I see that array is full of data while inside the function (see result below).
2. In the main function, First I call that function, and then i try to echo the array values. (But outside the function i.e, in main function it do not display value).

Result of using set -x
+ [ drivers= == drivers= ]
+ myarrayDriver[1]=/sbt/driver/RegressionTests/Test4_atg1


---------- Post updated at 06:22 AM ---------- Previous update was at 06:07 AM ----------

$var is also do not show result in main function.
# 5  
Old 09-02-2010
I would look at the assignment inside the function, if I do a simple test:
Code:
$ funcA(){ A=(5 6 7 8); } ; funcA ;echo ${A[2]}
7

The value is retained..

You are testing ${myarrayDriver[1]} . Are you sure ${myarrayDriver[0]} gets set as well?
# 6  
Old 09-02-2010
It should be no problem when using bash or ksh. Best post your whole code and tell us what shell you are using.

Here a simple example that shows that it is ok in bash and ksh:
Code:
#!//bin/bash

one()
{
        two
        echo inside one ${ARR[0]}
}

two()
{
        ARR[0]=bla
}

one

echo outside ${ARR[0]}

# 7  
Old 09-02-2010
I figured out the problem, please see the code marked in bold.
Silly mistakes Smilie

Nways, thanks for your help. Smilie

Code:
#!/bin/ksh


#================================================
#Reading Config file
#================================================
readingConfigFile()
{

search_keyword="$1"
i=0
for  pointer in $(cat wrapper_script.config)
do
        if [ "$search_keyword" == "drivers=" ]
        then
                var=$(echo $pointer |awk -F= '/'"$search_keyword"'/ {print $2}')
                if [ "$var" != "" ] && [ "$search_keyword" == "drivers=" ]
                        then
                        myarrayDriver[$i]=$var
                        i=$(expr $i + 1)
                fi
        fi
done
}
#================================================
#Main Function
#================================================

number_of_driver=$(readingConfigFile "drivers=")
echo "number_of_driver= $number_of_driver"
echo ${myarrayDriver[0]} #NO OUTPUT
readingConfigFile()
echo ${myarrayDriver[0]} #NO OUTPUT
readingConfigFile() "drivers="
echo ${myarrayDriver[0]} # Bingo

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

4. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

5. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

6. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

7. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

8. UNIX for Dummies Questions & Answers

passing variable to function

Hi, I am trying to sum up numbered columns and in order to tidy up the program I have wrote a function to do the adding of some numbers. I have a problem though with passing a variable to the function in the UNIX bash shell. The function only gives the first number in the variable list and does... (4 Replies)
Discussion started by: Knotty
4 Replies

9. Shell Programming and Scripting

variable not retaining value

Bourne shell Solaris I'm trying to set a flag to perform an action only when data is found. So I initialize the flag with: X=0 Then I read the data: if ; then while read a b c do X=1 done < ${inputFile} fi The problem is that X will be set to 1 inside the while loop but when... (5 Replies)
Discussion started by: gillbates
5 Replies

10. Shell Programming and Scripting

Parsing data and retaining the full length of variable

Here's is an example of what I want to do: var1="Horse " var2="Cat " var3="Fish " for animals in "$var1" "$var2" "$var3" do set $animals pet=$1 ## Ok, now I want to get the values of $pet, but ## I want to retain the full length it was... (3 Replies)
Discussion started by: app4dxh
3 Replies
Login or Register to Ask a Question