将所有变量从一个Shell脚本传递到另一个脚本? [英] Pass all variables from one shell script to another?

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

问题描述

让我们说我有一个名为test.sh的shell/bash脚本,其内容为:

Lets say I have a shell / bash script named test.sh with:

#!/bin/bash

TESTVARIABLE=hellohelloheloo
./test2.sh

我的test2.sh看起来像这样:

#!/bin/bash

echo ${TESTVARIABLE}

这不起作用.我不想将所有变量都作为参数传递,因为恕我直言,这太过分了.

This does not work. I do not want to pass all variables as parameters since imho this is overkill.

有其他方法吗?

推荐答案

您基本上有两种选择:

  1. 在执行第二个脚本之前,将变量设置为环境变量(export TESTVARIABLE).
  2. 获取第二个脚本,即. test2.sh,它将在同一shell中运行.这样一来,您可以轻松共享数组之类的更复杂的变量,而且还意味着其他脚本可以在源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.

更新:

要使用export设置环境变量,可以使用现有变量:

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

A=10
# ...
export A

这应该在bashsh中都起作用. bash还允许将其像这样进行组合:

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

export A=10

这也可以在 my sh中使用(碰巧是bash,您可以使用echo $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脚本的事实也是偶然的.可以将环境变量传递给您执行的任何进程,例如,如果我们使用python代替,则它看起来可能像这样:

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 中执行.注意,我们不必导出变量即可访问它.这隐式共享您拥有的所有变量,并允许其他脚本在外壳中添加/删除/修改变量.当然,在此模型中,两个脚本都应使用相同的语言(shbash).举个例子,我们如何来回传递消息:

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

这在bash中同样有效.它还使您可以轻松共享更复杂的数据,例如数组或关联数组,这些数据无法表达为环境变量(至少没有付出很大的努力).

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.

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

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