Shell脚本:将变量从一个shell脚本导出到另一个不使用“source / export”的shell脚本

时间:2017-08-21 17:15:38

标签: bash shell

我试图将变量从一个shell脚本调用到另一个。我使用“来源”和“出口”,但没有人在做这项工作。提一下,它是CGI bash脚本。在bash脚本中还有“”指向其他CGI脚本的HTML链接。例如 - 我这样做;

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>"

脚本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"

脚本3:test5.sh

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

但它在“test5.sh”中没有给我任何东西

所以,我使用HTML从“test1.sh”调用“test4.sh”,并将“test1.sh”的所有变量导出到“test4.sh”,如上所示。所有变量都很好地导出到“test4.sh”,但变量不会从“test1.sh”导出到“test5.sh”。我也想在“test5.sh”中使用“test1.sh”的变量。同样,我在同一目录中有其他bash脚本,我想从“test1.sh”中导出变量及其值。你能告诉我怎么办?我在“test5.sh”中尝试了上面的“source”,但它不起作用。如果您需要更多信息,请与我们联系。

注意:由于编程结构的原因,我不想在“test1.sh”脚本中使用“bash test5.sh”或“./test5.sh”从“test1.sh”直接调用“test5.sh” 。序列必须是“test1.sh将变量导出到test4.sh和test5.sh”

非常感谢你! DK

1 个答案:

答案 0 :(得分:1)

我会为查尔斯做咕噜咕噜的工作:

script0:vars.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>'

script1:test1.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

脚本2:test4.sh

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

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

脚本3:test5.sh

{{1}}