Solved

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

Posted on 2004-08-18
12
298 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
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…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

746 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

16 Experts available now in Live!

Get 1:1 Help Now