LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass a variable from parent to child script. (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-a-variable-from-parent-to-child-script-4175617535/)

zaeem 11-13-2017 08:08 AM

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

TenTenths 11-13-2017 08:10 AM

What have you tried so far?

zaeem 11-13-2017 08:14 AM

I have tried using export and it didn't work. I used 'source ./' but that is not what I require.

TenTenths 11-13-2017 08:18 AM

What's stopping you from passing the parameters as positional variables?

michaelk 11-13-2017 08:31 AM

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.

zaeem 11-13-2017 08:41 AM

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

TenTenths 11-13-2017 08:47 AM

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!


michaelk 11-13-2017 08:57 AM

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.

zaeem 11-13-2017 08:58 AM

Quote:

Originally Posted by TenTenths (Post 5780159)
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

TenTenths 11-13-2017 09:00 AM

Quote:

Originally Posted by zaeem (Post 5780163)
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

michaelk 11-13-2017 09:02 AM

As posted you can use the tee command

Code:

./child.sh "${v_Name}" | tee > /tmp/child.log


All times are GMT -5. The time now is 07:42 AM.