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
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.
I tried set -x, and I see that array is full of data while inside the function (see result below).
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 ----------