DanielSebas
DanielSebas

Reputation: 135

Shell script: exporting variables from one shell script to another shell script not working using "source/export"

I am trying to call variables from one shell script to other. I used "source" and "export" but none are doing the job. A mention, that it is CGI bash script. And in the bash script there is also "" HTML links to other CGI scripts. For example - I am doing like this;

script1: test1.sh

#!/bin/sh
echo "Content-type: text/html"
echo ""
....
export XX="1"
export YY="2"
"echo "<p><a href = "/path/to/second/CGI/script/test4.sh" >to choose click</a></p>"

script 2: test4.sh

#!/bin/sh
echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<body>"
echo "<form action="/path/to/third/CGI/script/test5.sh" method="GET">"
echo "</form>"
echo "</body>"
echo "</html>"
echo "XX =$XX"

script 3: test5.sh

#!/bin/sh
#source <(grep '^export .*=' test1.sh)
echo "Content-type: text/html"
echo ""
echo "XX = $XX"

but it gives me nothing in "test5.sh"

So, I called "test4.sh" from "test1.sh" using HTML and exported all variables of "test1.sh" to "test4.sh" as shown above. All the variables are exported well to "test4.sh" but the variables are not getting exported to "test5.sh" from "test1.sh". I want to use the variables of "test1.sh" in "test5.sh" also. Likewise, I have other bash scripts in the same directory, in which I would like to export the variables and their values from "test1.sh". Can you please let me know how can I do it? I tried "source" like above in "test5.sh" but it does not work. Please let me know if you need more information on this.

Note: I don't want to directly call "test5.sh" from "test1.sh" using "bash test5.sh"or "./test5.sh" in "test1.sh" script due to programming structure. The sequence has to be "test1.sh exports variables to test4.sh and test5.sh"

Thank you so much! DK

Upvotes: 0

Views: 413

Answers (1)

glenn jackman
glenn jackman

Reputation: 247210

I'll do the grunt work for Charles:

script0: vars.sh

XX="1"
YY="2"

script1: test1.sh

#!/bin/sh
echo "Content-type: text/html"
echo ""

## If some content in this file not shown in the example needs the values, then:
. /path/to/vars.sh

## These lines are no longer needed
# export XX="1"
# export YY="2"

echo '<p><a href = "/path/to/second/CGI/script/test4.sh" >to choose click</a></p>'

script 2: test4.sh

#!/bin/sh
. /path/to/vars.sh

cat <<"END_HTML"
Content-type: text/html

<html>
<body>
<form action="/path/to/third/CGI/script/test5.sh" method="GET">
</form>
<div>XX =$XX</div>
</body>
</html>
END_HTML

script 3: test5.sh

#!/bin/sh
. /path/to/vars/sh

echo "Content-type: text/plain"
echo ""
echo "XX = $XX"

Upvotes: 1

Related Questions