LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2017, 08:08 AM   #1
zaeem
Member
 
Registered: Jan 2010
Posts: 125

Rep: Reputation: 15
How to pass a variable from parent to child script.


Good Evening,

I want to create a bash script which will take some update from SVN and do some manipulations. I want to call that bash script from a wrapper bash script which will take few inputs from invoker like 'CustomerName' and 'BranchName' which needs to be passed to child bash script for actual manipulations and committing the changes back to SVN. I couldn't find a way to pass variables from parent to child bash script. I would be thankful if anybody can help. Code snippet of parent bash script is as follows;

#!/bin/sh
## parent.sh
echo Enter Your Name?
read v_Name
sh /home/test/child.sh > /tmp/child.log


I need to retrieve value of v_name in child script
 
Old 11-13-2017, 08:10 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,938

Rep: Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210
What have you tried so far?
 
Old 11-13-2017, 08:14 AM   #3
zaeem
Member
 
Registered: Jan 2010
Posts: 125

Original Poster
Rep: Reputation: 15
I have tried using export and it didn't work. I used 'source ./' but that is not what I require.
 
Old 11-13-2017, 08:18 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,938

Rep: Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210
What's stopping you from passing the parameters as positional variables?
 
Old 11-13-2017, 08:31 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Without knowing anything about you child script it difficult to provide help. Did you write the child script?

Does the child script accept input data? If so how?

There are many methods as suggested using command line arguments i.e positional parameters, prompting i.e. read statement, reading data from a file, reading data from stdin i.e. data from console/keyboard.

Last edited by michaelk; 11-13-2017 at 08:33 AM.
 
Old 11-13-2017, 08:41 AM   #6
zaeem
Member
 
Registered: Jan 2010
Posts: 125

Original Poster
Rep: Reputation: 15
I can pass positional variable as
Quote:
sh child.sh $v_Name
and it works. But I need to generate log as well and when I write it as
Quote:
sh child.sh $v_Name >/tmp/child.log
it doesn't work and seems like child.sh is not invoked. I am printing values in child.sh as

Quote:
#!/bin/bash

echo "Positional Parameters"
echo '$0 = ' $0
echo '$1 = ' $1
 
Old 11-13-2017, 08:47 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,938

Rep: Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210
Pretty much the same works fine for me.....

Code:
[user@web105 ~]# cat parent.sh
#!/bin/bash
## parent.sh
echo "Enter Your Name?"
read v_Name
./child.sh "${v_Name}" > /tmp/child.log


[user@web105 ~]# cat child.sh
#!/bin/bash
## child.sh

echo $1


[user@web105 ~]# ./parent.sh
Enter Your Name?
Test User Bob!

[user@web105 ~]# cat /tmp/child.log
Test User Bob!
 
1 members found this post helpful.
Old 11-13-2017, 08:57 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
In addition,

$0 is how the script was called.
$1 is the first command line argument.

Since you redirect all output to a file you will not see output on the console. Have you check the child.log file?

As TenTenths posted using "${v_Name}" will pass the name as one argument otherwise if there is a space in the name it will actually be parsed as two by the child.
 
1 members found this post helpful.
Old 11-13-2017, 08:58 AM   #9
zaeem
Member
 
Registered: Jan 2010
Posts: 125

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by TenTenths View Post
Pretty much the same works fine for me.....

Code:
[user@web105 ~]# cat parent.sh
#!/bin/bash
## parent.sh
echo "Enter Your Name?"
read v_Name
./child.sh "${v_Name}" > /tmp/child.log


[user@web105 ~]# cat child.sh
#!/bin/bash
## child.sh

echo $1


[user@web105 ~]# ./parent.sh
Enter Your Name?
Test User Bob!

[user@web105 ~]# cat /tmp/child.log
Test User Bob!
It is working. Thanks. Is there anyway we can print the output of child.sh on the screen as well. Currently it is only writing in /tmp/child.log
 
Old 11-13-2017, 09:00 AM   #10
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,938

Rep: Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210Reputation: 1210
Quote:
Originally Posted by zaeem View Post
It is working. Thanks. Is there anyway we can print the output of child.sh on the screen as well. Currently it is only writing in /tmp/child.log
Code:
man tee
 
1 members found this post helpful.
Old 11-13-2017, 09:02 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
As posted you can use the tee command

Code:
./child.sh "${v_Name}" | tee > /tmp/child.log
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to pass value from a shell script run across ssh back to the parent script? holpot Linux - Newbie 2 05-09-2017 02:55 PM
fork(), variable values between child and parent process???? dearvivekkumar Programming 6 02-24-2010 05:18 AM
Child can't see parent's environment variable bcarson Linux - Newbie 5 10-13-2008 09:47 AM
How to share the same variable between parent and child process? zwylinux Programming 6 03-26-2007 02:47 AM
GCC fork() - Can a child change a parent variable? johnhardey Programming 4 03-10-2005 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration