Problem in Global variables in shell script

hi,

i have a shell script which calls another shell which in turn calls another shell script.

Main_shell_script.sh

echo "In Main_shell_script.sh"
FILENAME="original.txt"         # LINE 1

DST_FILENAME=$FILENAME   # LINE 2
echo "FILENAME = {$FILENAME}"
echo "DST_FILENAME = {$DST_FILENAME}"

. /home/shell_script_A.sh      # Calling to execute in the same address space.

DST_FILENAME=$FILENAME   #LINE 3
echo "In Main_shell_script.sh"
echo "FILENAME = {$FILENAME}"        # prints blank
echo "DST_FILENAME = {$DST_FILENAME}"    # prints blank

shell_script_A.sh

echo "In shell_script_A.sh"

FILENAME=""                        # clearing the FILENAME
echo "FILENAME = {$FILENAME}

. /home/shell_script_B.sh

shell_script_B.sh

echo "In shell_script_B.sh"
FILENAME="sample.txt"        # reassigning the FILENAME
echo "FILENAME = {$FILENAME}"

in the above 3 scripts,
when i am assigning a different value to variable "FILENAME" in shell_script_B.sh script, its getting assigned properly and the value of the FILENAME is "sample.txt". but when the control is returned back to the main_shell_script.sh, the value in the variable FILENAME is blank. so after assigning the FILENAME to DST_FILENAME, DST_FILENAME contains nothing.

But according to the program since i am calling each shell script with a dot (.) in front, it should run in the same address space. so the variables declared in the main shell script should be visible to the other shell script and changing the value of the variable in any shell script should reflect in the other scripts also.
but, when i change the value of FILENAME in shell_script_B.sh, the changed value is not reflected in the main_shell_script.sh.
Can anyone tell where i am going wrong??

here is the output i am getting

In Main_shell_script.sh
FILENAME = {original.txt}
DST_FILENAME = {original.txt}

In shell_script_A.sh
FILENAME = {}

In shell_script_B.sh
FILENAME = {sample.txt}

In Main_shell_script.sh
FILENAME = {}
DST_FILENAME = {}

here is what i got after i added the missing " in shell_script_A.sh ...

FILENAME=""                        # clearing the FILENAME
echo "FILENAME = {$FILENAME}"
[otto@centosgeek ~]$ ./Main_shell_script.sh
In Main_shell_script.sh
FILENAME = {original.txt}
DST_FILENAME = {original.txt}
In shell_script_A.sh
FILENAME = {}
In shell_script_B.sh
FILENAME = {sample.txt}
In Main_shell_script.sh
FILENAME = {sample.txt}
DST_FILENAME = {sample.txt}
[otto@centosgeek ~]$ 
1 Like

ok i got whats the problem.. i need your help to fix it.

actually reassigning the FILENAME inside a while loop. so inside the while loop its getting assigned but as soon as it comes out of the loop, it loses its value. so again it becomes blank. how to fix that??

You may want to export the variable when the loop ends (this is a simple example since you did not post the code of your while loop):
main.sh:

#!/bin/bash
clear
echo "Starting execution of main script..."
i=1
while [ $i -le 5 ]
do
        echo $i
            (( i++ ))
            done
export i
echo "Execution of main script OK. Now calling script_a.sh..."
./script_a.sh $i

script_a.sh:

#!/bin/bash
echo "This is script_a.sh..."
echo $1

The output:

Starting execution of main script...
1
2
3
4
5
Execution of main script OK. Now calling script_a.sh...
This is script_a.sh...
6
gacanepa@debian:~/scripts/bash/tests$

Hope it helps. If you consider that your question has been answered, please consider marking this thread as solved.