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:

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

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.

Stupid question, but where do you call the function in your script?
In the statement

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

if [ -n "$var" ] # string contains something

If you really call the function, an echo "$var" should give you something

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.

I would look at the assignment inside the function, if I do a simple test:

$ 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?

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:

#!//bin/bash

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

two()
{
        ARR[0]=bla
}

one

echo outside ${ARR[0]}

I figured out the problem, please see the code marked in bold.
Silly mistakes :wink:

Nways, thanks for your help. :b:

#!/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

Post withdrawn - crossed with O/P post.