Solved

how to acess a data which is set in child shell script from a parent script.

Posted on 2004-08-18
12
301 Views
Last Modified: 2010-04-21
Hi ,

I have a problem ...

I am using ksh , solaris2.8
I am executing some shell scripts in follwoing manner .

       ABC.sh
          Inside this(ABC.sh)  script i am execution
          . BCD.sh ---> in BCD.sh some variables are set and done a export of those variables.
          . XYZ.sh  --> this script executed inside ABC.sh and should acess the variables set by BCD.sh


How to do this. I exported variable in BCD.sh but I am not able to acess those in XYZ.sh .

Please give some idias.

Thanks
Raghava
 
0
Comment
Question by:raghava_dg
12 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 11836901
You can't, at least the not way you are going about it. When BCD.sh is executed it will be run by a child shell of ABC.sh. Since env variables are inherited downwards only any variable set during the execution of BCD.sh will disappear when that shell exits. What you could do would be to 'source BCD.sh', which will execute the commands in that file within the current shell. That way any variables set by BCD.sh will exist in the current shell and will thus be inheritied by the shell that executes XYZ.sh.

0
 
LVL 11

Expert Comment

by:avizit
ID: 11837231
As a last resort if you have no other way you can do the following

in the child script when you set the value you also write a file

as iin

echo "set ......." >  /tmp/test.tmp

and in parent script  read from that file.

Be warned that you can run into host of other issues with this


ps: this is not a good way to do it and not recommended ;)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11838346
If you are calling BCD.sh as you showed, then there should be know problem.  Observe

$ ls
ABC.sh  BCD.sh  XYZ.sh

$ cat ABC.sh
#!/bin/sh
echo start $0
. BCD.sh
./XYZ.sh
echo finish $0

$ cat BCD.sh
#!/bin/sh
FOO=bar
export FOO

$ cat XYZ.sh
#!/bin/sh
echo "FOO = $FOO"

$ ./ABC.sh
start ./ABC.sh
FOO = bar
finish ./ABC.sh


If you changed ABC.sh to be:

#!/bin/sh
echo start $0
. BCD.sh
. XYZ.sh
echo finish $0

You wouldn't need to export FOO in BCD.sh, as it is effectively running in the same shell.


0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:raghava_dg
ID: 11845948
yes , as said by Tintin  it should work . I tried a sample prog and ran on the same machine on same shell , it is worikng . BUt when you try with the real script which i am writing it is not working . the variable are no where resetting . going crazy !!!!.
0
 

Author Comment

by:raghava_dg
ID: 11846923
now i narrowed down the problem .

i am having a while loop and inside while loop i am setting and exporting some variables . those variables are available inside while loop . But if I come out from while loop these variables are resetting to blank . In b/w these I am not resetting any PATH or variables .

my code is some thing like this.



ITERATE_FLAG=TRUE
while read tmpLine
do
      #Ignore any lines with hash
      line=`echo $tmpLine | egrep -v -e "#"`
      echo "Line " $line
      #Ignore the blank lines
      if [ "EMT$line" != "EMT" ]
      then
         if [ "$ITERATE_FLAG" = "TRUE" ]
         then
            ......do some stuff here ........
         fi
        fi

done < $CONFIG_FILE

I wrote a sample prog of while loop reading a file it is working good .

any suggestions/ideas to go abt it will be highly appreciated .

thanks
raghava
0
 

Author Comment

by:raghava_dg
ID: 11849126
guyz I found the solution. But I am not sure why it is behaving like this . If any 1 knows please let us know.

Solution:..

I used

execute < $CONFIG_FILE

while read tmpLine
do
....
..
done


I used this code insted of prev one.

Thanks for helping.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11850327
are you shure you have not done it in a subshell somehow?
like using: ( commad whatever )
0
 
LVL 5

Expert Comment

by:ITcrow
ID: 11865398
The simplest solution in your case will be to write out a file  in /tmp in .kshrc style.
Source it from the parent script and then delete the file. This should set all your
variables in the file.

At the same time, if you leave the file in /tmp, it will help you debug the problem,
if your variables are not being set correctly. Visual inspection of file and repeating
the steps manually should reveal the problem.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 20 total points
ID: 11866723
From the shell FAQ.

36. Why do I lose the value of global variables that are set in a loop.

    Given the following program

      #!/bin/sh
      x="this is the initial value of x"
      cat dataFile | while read line;do
        x="$line"
      done
      echo x = $x

     You may get the following for output

       x = this is the initial value of x
   
     This is because in the Bourne shell redirected control structures
     run in a subshell, so the value of x only gets changed in the
     subshell, and is lost when the loop ends.

     In other shells the same result may be seen because of the way
     pipelines are handled. In shells other than ksh (not pdksh) and
     zsh elements of a pipeline are run in subshells. In ksh and zsh,
     the last element of the pipeline is run in the current shell.

     An alternative for non-Bourne shells is to use redirection
     instead of the pipeline

      #!/bin/sh
      x="this is the initial value of x"
      while read line;do
        x="$line"
      done < dataFile
      echo x = $x

    With a Bourne shell you need to reassign file descriptors, so no
    pipline or redirection in the loop is involved.

      exec 3<&0         # save stdin
      exec < file
      while read line; do
        x=$line
      done
      exec 0<&3        # restore stdin

    Note that putting #!/bin/sh at the top of a script doesn't
    guarantee you're using the Bourne shell. Some systems link /bin/sh
    to some other shell. Check your system documentation to find out
    what shell you're really getting in this case.
0
 

Author Comment

by:raghava_dg
ID: 11877467
Thats a good explanation . I will give point to TinTin .
0
 

Author Comment

by:raghava_dg
ID: 11877471
Hey TinTin one more thing .Can you please tell us where did u get this FAQ from ?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11899277
Easiest place to get all the FAQ's is from http://www.faqs.org/
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now