通过从一个shellscript里所有变量到另一个? [英] Pass all variables from one shellscript to another?

查看:96
本文介绍了通过从一个shellscript里所有变量到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,可以说,我有一个shellscript里/ bash脚本命名test.sh

So lets say i have a shellscript / bash script named test.sh

#!/bin/bash
#

TESTVARIABLE=hellohelloheloo
./test2.sh

我test2.sh看起来像这样

my test2.sh looks like this

#!/bin/bash
#

echo ${TESTVARIABLE}

这是行不通的。我并不想通过所有的变量作为参数,因为恕我直言,这是矫枉过正。是否有不同的方式?

this does not work. I do not want to pass all variables as parameters since imho this is overkill. Is there a different way?

推荐答案

您基本上有两种选择:


  1. 执行第二脚本之前,请为变量的环境变量(出口TESTVARIABLE )。

  2. 源2号的脚本,即。 test2.sh ,它会在同一个shell中运行。这将让你分享如数组更复杂的变量容易,同时也意味着其他脚本可以在源外壳修改变量。

  1. Make the variable an environment variable (export TESTVARIABLE) before executing the 2nd script.
  2. Source the 2nd script, i.e. . test2.sh and it will run in the same shell. This would let you share more complex variables like arrays easily, but also means that the other script could modify variables in the source shell.

更新:

要使用导出来设置环境变量,您可以使用现有的变量:

To use export to set an environment variable, you can either use an existing variable:

A=10
# ...
export A

本应该在这两个庆典 SH 工作。 庆典也允许它像这样被组合:

This ought to work in both bash and sh. bash also allows it to be combined like so:

export A=10

这也适用于的我的 SH (这恰好是庆典,你可以使用回声$ SHELL 检查)。但我不认为这是保证在所有工作 SH ,所以最好的发挥它的安全和他们分开。

This also works in my sh (which happens to be bash, you can use echo $SHELL to check). But I don't believe that that's guaranteed to work in all sh, so best to play it safe and separate them.

您以这种方式出口的变量都会在执行脚本可见,例如:

Any variable you export in this way will be visible in scripts you execute, for example:

a.sh:

#!/bin/sh

MESSAGE="hello"
export MESSAGE
./b.sh

b.sh:

#!/bin/sh

echo "The message is: $MESSAGE"

然后:

$ ./a.sh
The message is: hello

这些都是shell脚本的事实也只是偶然。环境变量可以传递给你执行任何程序,例如,如果我们用蟒蛇代替它可能看起来像:

The fact that these are both shell scripts is also just incidental. Environment variables can be passed to any process you execute, for example if we used python instead it might look like:

a.sh:

#!/bin/sh

MESSAGE="hello"
export MESSAGE
./b.py

b.py:

#!/usr/bin/python

import os

print 'The message is:', os.environ['MESSAGE']

采购:

相反,我们可能来源是这样的:

Instead we could source like this:

a.sh:

#!/bin/sh

MESSAGE="hello"

. ./b.sh

b.sh:

#!/bin/sh

echo "The message is: $MESSAGE"

然后:

$ ./a.sh
The message is: hello

这或多或少的进口 b.sh 的内容直接在同一个shell执行它的。请注意,我们没有出口的变量进行访问。这隐含共享所有你的变量,以及允许其他脚本在shell添加/删除/修改变量。当然,在这种模式既脚本应该是相同的语言( SH 庆典)。举个例子,我们怎么能传递消息来回:

This more or less "imports" the contents of b.sh directly and executes it in the same shell. Notice that we didn't have to export the variable to access it. This implicitly shares all the variables you have, as well as allows the other script to add/delete/modify variables in the shell. Of course, in this model both your scripts should be the same language (sh or bash). To give an example how we could pass messages back and forth:

a.sh:

#!/bin/sh

MESSAGE="hello"

. ./b.sh

echo "[A] The message is: $MESSAGE"

b.sh:

#!/bin/sh

echo "[B] The message is: $MESSAGE"

MESSAGE="goodbye"

然后:

$ ./a.sh
[B] The message is: hello
[A] The message is: goodbye

这同样适用于庆典。它也可以很容易地分享更为复杂的数据,你不能EX preSS作为环境变量(至少在没有你做一些繁重的),如数组或关联数组。

This works equally well in bash. It also makes it easy to share more complex data which you could not express as an environment variable (at least without some heavy lifting on your part), like arrays or associative arrays.

这篇关于通过从一个shellscript里所有变量到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆