简体   繁体   English

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

[英]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. 我正在尝试从一个shell脚本调用变量到另一个。 I used "source" and "export" but none are doing the job. 我使用了“源”和“导出”,但是没有一个在做。 A mention, that it is CGI bash script. 一提,那是CGI bash脚本。 And in the bash script there is also "" HTML links to other CGI scripts. 在bash脚本中,还存在指向其他CGI脚本的HTML链接。 For example - I am doing like this; 例如-我这样做

script1: test1.sh 脚本1: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 脚本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 脚本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" 但是它在“ 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. 因此,我使用HTML从“ test1.sh”调用了“ test4.sh”,并将“ test1.sh”的所有变量导出到“ test4.sh”,如上所示。 All the variables are exported well to "test4.sh" but the variables are not getting exported to "test5.sh" from "test1.sh". 所有变量都很好地导出到“ test4.sh”,但是变量没有从“ test1.sh”导出到“ test5.sh”。 I want to use the variables of "test1.sh" in "test5.sh" also. 我也想在“ test5.sh”中使用“ test1.sh”的变量。 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". 同样,我在同一目录中还有其他bash脚本,我想在其中从“ 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. 我在“ test5.sh”中尝试了如上的“源”,但是它不起作用。 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. 注意:由于编程结构的原因,我不想使用“ test1.sh”脚本中的“ bash test5.sh”或“ ./test5.sh”直接从“ test1.sh”中调用“ test5.sh”。 The sequence has to be "test1.sh exports variables to test4.sh and test5.sh" 序列必须为“ test1.sh将变量导出到test4.sh和test5.sh”

Thank you so much! 非常感谢! DK DK

I'll do the grunt work for Charles: 我将为查尔斯做艰苦的工作:

script0: vars.sh script0:vars.sh

XX="1"
YY="2"

script1: test1.sh 脚本1: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 脚本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 脚本3:test5.sh

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM