Passing a variable from a child script back to the parent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a variable from a child script back to the parent
# 1  
Old 05-26-2008
Passing a variable from a child script back to the parent

Hi

I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script are hard coded into the child script is it possible to pass these files names from the child back to the parent. I have seen some error checking examples but no examples of variable being passed back.

Any help would be greatly appreciated.
# 2  
Old 05-26-2008
You can find an example here:

https://www.unix.com/shell-programmin...-subshell.html

Regards
# 3  
Old 05-26-2008
Quote:
Originally Posted by Andy82
Hi

I have written a script using ftp to get files from one server and copy them to 2 dirrerent servers. I wish to call this script from a parent script that will check the number of files copied and run a check sum for each file. As the filenames for the files in the get portion of the script are hard coded into the child script is it possible to pass these files names from the child back to the parent. I have seen some error checking examples but no examples of variable being passed back.

Any help would be greatly appreciated.

Andy, If you use ksh93 this will work.

Stick your child scripts into functions - or at least have functions which run the child scripts using the . operator so that they are running within the same process as the main script.

then do something like below:
------------------------------------------

function afunc
{
# This is the function which either contains the child script or calls it using # the . operator

# the line below makes parameter 1 a by reference parameter
typeset -n pFilename=$1
..
..
.. script sets vValue to a filename received by FTP somewhere in here
.. for example vValue="receivedfile.txt"
pFilename=$vValue

return 0
}

# The main script is here
..
..
..
# lets call the function
afunc vFilename
# note that vFilename = $1 in afunc and will point to the same memory address as pFilename variable in the function
echo $vFilename
# which miraculously will turn out to be "receivedfile.txt"
-------------------------------------------------------------

You could do something similar with ksh bash or sh but that would work because all the variables of scripts using the same process are global - some versions of bash may have ksh93 features though.

If you must use scripts which don't use the same processes then the following will work:

#---- Calling script--------
..
..
# call the child script
childscript.sh
avar=`cat afile`
echo $avar
..
..
#---end of calling script-------------------

#---childscript.sh---------------
..
.. bvar="receivedfile.txt" happens somewhere in here
..
echo $bvar > afile
#------ end of childscript.sh
# 4  
Old 05-26-2008
Another way is to use a named pipe to communicate between the 2 scripts. Do a Web search for "shell named pipe".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fail Parent Script if any of the child fails

I have requirement where I need to fail parent if any one of the child process fails. Here is the code snippet for i in 1 2 3 4 5 6 7 8 9 10 do child_script $i & done wait I need to fail my main script if any one of my child process fails (8 Replies)
Discussion started by: gvkumar25
8 Replies

2. Shell Programming and Scripting

Passing key column from parent to child records

Hi Forum. I have this challenging issue that I'm hoping someone can help me. I have a file that contains 3 different types of segments (AM00, AM01, AM32) in a hierarchy structure and I want to be able to pass the column key from the parent record to the children records. AM00 - parent key:... (13 Replies)
Discussion started by: pchang
13 Replies

3. Shell Programming and Scripting

How to exit from the parent script while the child is running?

hi, i want to call a child shell script from a parent shell script. the child will be running for 5 mins. normally when the child is running, parent will wait till the child completes. so in the above case parent will be paused for 5 mins. is there a way so that the parents does not wait for the... (3 Replies)
Discussion started by: Little
3 Replies

4. IP Networking

Message passing from child to parent using pipes

Hi, I am trying my hand in networking programming in C, and got stuck in piping. I was following some tutorial and did the forking like : while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) ... (4 Replies)
Discussion started by: abhi1988sri
4 Replies

5. Shell Programming and Scripting

Child exiting and not returning to parent script

I am having a parent scripts which reads a file with child scripts name. I need to read one by one child script , execute it and 1. If child script fails send mail to the team with the log file 2. If the child script executes fine then proceed with the next child script execution. #!... (3 Replies)
Discussion started by: nw2unx123
3 Replies

6. Shell Programming and Scripting

Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue: I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh ... (3 Replies)
Discussion started by: gvolpini
3 Replies

7. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

8. Shell Programming and Scripting

Passing variable to child instance of make

hi, I am trying to export a variable from the parent makefile to a child instance of my makefile within the same directory. the relevant code in my parent makefile is: libvar := $(notdir $(basename ../src/file1)) export libvar .PHONY: library library: make -f makechild libvar=file1... (0 Replies)
Discussion started by: bacpp
0 Replies

9. Shell Programming and Scripting

Script, child kills parent

Hello everyone, I'm trying to write a script, i would like to say the child kills the parent, how would i do that? (2 Replies)
Discussion started by: jessy21
2 Replies

10. Shell Programming and Scripting

How to export a variable from a child process running in background to the parent

Hi All, I have a script which calls a child script with a parameter to be run in the background . childscript.ksh $a & Can any one suggest me how do i export a variable from the child script to parent script? Note that the child script is in background If the child script is in... (3 Replies)
Discussion started by: aixjadoo
3 Replies
Login or Register to Ask a Question