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

查看:59
本文介绍了将所有变量从一个 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 看起来像这样:

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.

有什么不同的方法吗?

推荐答案

你基本上有两个选择:

  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 中执行它.请注意,我们不必导出变量即可访问它.这隐含地共享您拥有的所有变量,并允许其他脚本在 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天全站免登陆