bash 在shell脚本之间导出变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5677446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting variables between shell script
提问by thetux4
I have two independently running scripts. first one lets say script A calculates some values. And i want to echo these values from other script called B. These scripts will not call each other. I have used export keyword but didn't work. How can i do this?
我有两个独立运行的脚本。第一个假设脚本 A 计算一些值。我想从其他名为 B 的脚本中回显这些值。这些脚本不会相互调用。我使用了 export 关键字但没有用。我怎样才能做到这一点?
采纳答案by anubhava
If I understood the requirement then can't both scripts be simply executed in same sub shell, independently but withoutcalling each other or without any need of an external file or pipe like this:
如果我理解要求,那么两个脚本就不能简单地在同一个子 shell 中执行,独立但不相互调用或不需要像这样的外部文件或管道:
Let's say this is your script1.sh
假设这是您的script1.sh
#!/bin/bash
# script1.sh: do something and finally
export x=19
And here us your script2.sh
这里是你的script2.sh
#!/bin/bash
# script2.sh: read value of $x here
echo "x="${x}
Just call them in same sub-shell like this
只需像这样在同一个子外壳中调用它们
(. ./script1.sh && ./script2.sh)
OUTPUT:
输出:
x=19
回答by sehe
mkfifo /tmp/channel
process_a.sh > /tmp/channel&
process_b.sh < /tmp/channel&
wait
Of course you can also just read a single line when you want it.
当然,您也可以在需要时只读取一行。
In bash there are coprocs, which also might be what you want. Random example from this page
在 bash 中有 coprocs,这也可能是您想要的。此页面的随机示例
# let the output of the coprocess go to stdout
{ coproc mycoproc { awk '{print "foo" #!/bin/sh
# a.sh: Calculate something and return the result
echo 19
;fflush()}' ;} >&3 ;} 3>&1
echo bar >&${mycoproc[1]}
foobar
ksh has a similar feature, apparently
ksh 有一个类似的功能,显然
回答by Hai Vu
Think of each script as a function: function A calculates some value and returns it. It does not know who will call it. Function B takes in some value and echo it. It does not care who produced that value. So, script A is:
将每个脚本视为一个函数:函数 A 计算一些值并返回它。它不知道谁会打电话给它。函数 B 接收一些值并回显它。它不关心谁产生了那个价值。所以,脚本A是:
#!/bin/sh
# b.sh: Consume the calculated result, which passed in as
echo The result is
and script B is:
和脚本 B 是:
chmod +x [ab].sh
Make them executable:
使它们可执行:
$ b.sh $(a.sh)
The result is 19
Now, we can glue them together on the command line:
现在,我们可以在命令行上将它们粘合在一起:
# common.sh
export MY_PATH=/home/foo/bar/
export VERSION=42.2a
Semantically, b.sh did not call a.sh. You called a.sh and passed its result to b.sh.
在语义上,b.sh 没有调用 a.sh。您调用 a.sh 并将其结果传递给 b.sh。
回答by Aif
Can't you read a third file, let's say settings.sh with the common exported variables?
你不能读取第三个文件,让我们说 settings.sh 与常见的导出变量?
echo `cat storedvalue`
and in both A and B source common.sh
to load those values.
并在 A 和 Bsource common.sh
中加载这些值。
Note that the export may not be required in that case.
请注意,在这种情况下可能不需要导出。
回答by ern0
You may echo values to files, then the other script may read them. If you want to use it as a parameter for something, use reverse aposthrophe:
您可以将值回显到文件中,然后其他脚本可以读取它们。如果您想将其用作某事的参数,请使用反撇号:
THIS_DIR=`dirname LOCAL_ROOT="$HOME/folder/folder"
REMOTE_USERNAME='someuser'
BACKUP_FOLDER="$LOCAL_ROOT/folder"
# etc.
`
source $THIS_DIR/config_vars.sh
# other commands
Be careful, if the two scripts are running at the same time, concurrency problems may occur, which can cause rarely appearing, mystic-looking errors.
请注意,如果两个脚本同时运行,可能会出现并发问题,这会导致很少出现、看起来很神秘的错误。
回答by doublejosh
Actually all your need is source
you can skip the export
prefix.
实际上,您只需要source
跳过export
前缀即可。
My use-case was an environment specific settings file, example:
我的用例是环境特定的设置文件,例如:
Within main_script.sh
在main_script.sh 中
##代码##Within config_vars.sh
在config_vars.sh 中
##代码##