Problem in Global variables in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in Global variables in shell script
# 1  
Old 07-04-2013
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

Code:
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

Code:
echo "In shell_script_A.sh"

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

. /home/shell_script_B.sh

shell_script_B.sh
Code:
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

Code:
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 = {}


Last edited by Little; 07-04-2013 at 04:01 AM..
# 2  
Old 07-04-2013
here is what i got after i added the missing " in shell_script_A.sh ...
Code:
FILENAME=""                        # clearing the FILENAME
echo "FILENAME = {$FILENAME}"

Code:
[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 ~]$

This User Gave Thanks to Just Ice For This Post:
# 3  
Old 07-04-2013
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??
# 4  
Old 07-04-2013
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:
Code:
#!/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:
Code:
#!/bin/bash
echo "This is script_a.sh..."
echo $1

The output:
Code:
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.
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 write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

Passing Global Shell variables to awk

Hi All, Iam trying to pass global shell variables and is not working Main script is like below CYEAR=`date +"%y"` CFYEAR=`date +"%Y"` CMONTH=`date +"%m"` if then PMONTH=12 PYEAR=`expr $CYEAR - 1` PFYEAR=`expr $CFYEAR - 1` else PMONTH=`expr... (6 Replies)
Discussion started by: baanprog
6 Replies

4. Shell Programming and Scripting

Problem while concatenating variables in shell script

Hi folks, I am facing problem when I concat variables with the string. Value for 'JDBC_CLASSES' variable looks malformed (/classes12.zip2.0KAGES) But, my expected result for 'JDBC_CLASSES' is /opt/API-R111/PACKAGES/jdbc/ORACLE9.2.0/classes12.zip Am I missing anything here? My... (10 Replies)
Discussion started by: Adhil
10 Replies

5. Solaris

How to access ENV variables of non global zones in global zone???

Hi Guys, My requirement is I have file called /opt/orahome/.profile in non global zone. PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:. export PATH PS1="\${ORACLE_SID}:`hostname`:\$PWD$ " export PS1 EDITOR=vi export EDITOR ENV=/opt/orahome/.kshrc export ENV... (1 Reply)
Discussion started by: vijaysachin
1 Replies

6. Shell Programming and Scripting

Global alias does not work in shell script

Hi Linux Set up - alias ls='ls -l' Then run script #! /bin/ksh sub() { ls } sub Is there any way to get it working. I don't want to define alias inside of the program Thank you (2 Replies)
Discussion started by: zam
2 Replies

7. UNIX for Dummies Questions & Answers

global variables

Hi, I hav created a script that calls a sub-script. In both the scripts i called the configuration file. Now i wanted to use a variable that should be used in both script and sub-script files. Actually, i wanted to return a file name and the return status to the script file from the sub-script.... (6 Replies)
Discussion started by: Swapna173
6 Replies

8. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies

9. Shell Programming and Scripting

Problem with global and local variables

Guys, how can I define global variables in sorlaris...cause I lose the values outside the scope. Rite now wat I do is,I redirect variable value to a file n then get it back outside the function...:o....theres obviously a better way of doing this...I now this is a basic question....but please... (2 Replies)
Discussion started by: qzv2jm
2 Replies

10. UNIX for Dummies Questions & Answers

How to declare global variables for shell script

Hi, I do have some variables accessed in more than one script. I want to have those variables in a command file anduse them. Something like a header file that we use in C programs. I dont want to set them as environment variables. Is there any other option, like header file in scripting ?? ... (2 Replies)
Discussion started by: risshanth
2 Replies
Login or Register to Ask a Question