将一个 shell 脚本变量传递给另一个 shell 脚本

发布于 2024-11-25 11:38:11 字数 441 浏览 5 评论 0原文

我正在尝试从另一个脚本访问一个脚本变量,例如

script1.sh:

export a=10
export b=20
echo "testing"
exit

script2.sh:

. script1.sh
echo $a

这里涉及的问题是它能够访问变量“a”来自script2中的script1,但它执行script1.sh中编写的所有命令,这很烦人。我只想访问 script1 中导出的变量,并且不想在 script2 中调用该命令时运行 script1 中的命令。

请帮忙!

谢谢,
卡蒂克

I am trying to access one script variable from another script for example,

script1.sh:

export a=10
export b=20
echo "testing"
exit

script2.sh:

. script1.sh
echo $a

The problem involved here is its able to access the variable 'a' from the script1 in script2 but its executing all the commands written in script1.sh which is annoying. I just want to access only the exported variables in script1 and donot want to run the commands in script1 while calling that in script2.

Kindly help!

Thanks,
Karthik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清泪尽 2024-12-02 11:38:11

您尝试执行的操作(仅从另一个脚本获取变量,而不执行命令)是不可能的。

这是一个解决方法:

将变量声明和初始化提取到第三个文件,然后由两个文件获取该文件。

common-vars:

export a=10
export b=20

script1.sh:

. common-vars
echo "testing"
exit

script2.sh:

. common-vars
echo $a

What you are trying to do (only get the variables from another script, without executing the commands) is not possible.

Here is a workaround:

Extract the variable declaration and initialisation to a third file, which is then sourced by both.

common-vars:

export a=10
export b=20

script1.sh:

. common-vars
echo "testing"
exit

script2.sh:

. common-vars
echo $a
脱离于你 2024-12-02 11:38:11

如果您的变量赋值采用上述精确形式,那么

export a=10

您可以使用grepeval提取这些赋值 结果。你的 script2.sh 将会是

eval `grep "^export " script1.sh`
echo $a

但是这样做非常脆弱,并且可能会以多种方式破坏。正如其他人所建议的那样,将变量分配放入由双方提供的第三个脚本中要安全得多。

如果您在条件语句中有变量设置,或者您导出的变量依赖于其他丢失的非导出变量,或者您单独设置和导出变量,则这可能会中断;如果您碰巧在导出中使用命令替换,那么它们仍然会运行,并可能产生副作用。

if [ $doexport = 1 ]; then
  export a=1
fi

a=2
export a

b=2
export a=$b

export a=`ls |wc -l`

这些都是潜在的问题。

If your variable assignments are only ever in the exact form indicated above, that is

export a=10

then you could extract those assignments with grep and eval the results. Your script2.sh would then be

eval `grep "^export " script1.sh`
echo $a

But doing this is very fragile, and can break in lots of ways. Putting the variable assignments in a third script sourced by both as others have suggested is far safer.

This can break if you have variable settings inside conditionals, or if your exported variables depend on other non-exported variables that got missed, or if you set and export the variables separately; and if you happen to be using command substitution in your exports those would still get run with their possible side effects.

if [ $doexport = 1 ]; then
  export a=1
fi

a=2
export a

b=2
export a=$b

export a=`ls |wc -l`

These are all potentially problematic.

好菇凉咱不稀罕他 2024-12-02 11:38:11

分配变量是一个命令。除非你准备在 bash 中编写一个 bash 解析器,否则你想要的东西是无法完成的。

Assigning a variable is a command. Unless you're prepared to write a bash parser in bash, what you want cannot be done.

ペ泪落弦音 2024-12-02 11:38:11

script2.sh:

eval "$(grep export script1.sh)"
echo $a

script2.sh:

eval "$(grep export script1.sh)"
echo $a
梦罢 2024-12-02 11:38:11

我同意伊格纳西奥·巴斯克斯·艾布拉姆斯的观点。

分配变量是一个命令

您对此无能为力。

一个可能的解决方案是将变量定义分离到第三个脚本,该脚本将用作配置脚本。因此,script1script2 都会source 那个脚本,以使用那里定义的变量。

I agree with Ignacio Vazquez-Abrams.

Assigning a variable is a command

not much you can do about it.

A probable solution would be to seperate the variable definitions to a third script, that would be used as configuration script. So both script1 and script2 would source that one, to use the vars defined there.

浅紫色的梦幻 2024-12-02 11:38:11

您可以通过将变量作为参数传递来将变量从一个脚本传递到另一个脚本。
这当然意味着脚本 B 将被脚本 A 调用。脚本 B 需要检查传递的参数是否不为空。

You can pass the variable from one script to another by passing the variable as a parameter.
This of course means that Script B will be called by Script A. Script B will need to check that the parameter passed is not empty.

腹黑女流氓 2024-12-02 11:38:11

我同意“Ignacio Vazquez-Abrams”的观点,这是对你的问题的一个很好的答案。

但我为您提供了另一种解决方案,您无需导出变量。概念基于:使用您的登录 shell 来运行脚本而不是新的 shell。

假设您的 script1.sh 是:

a=10 

b=20

现在在登录 shell 中运行您的脚本:

示例:

.[space]script1

现在,如果您将访问脚本中的变量 a,您将可以访问它,而不会失败。

示例

假设您的 script2.sh 是:

echo $a

运行:

.[space]script2

I agree with "Ignacio Vazquez-Abrams" this is a good answer to your question.

But I have another solution for you where you don't have export your variables. concept is based on : use you Login shell to run both the script instead of a new shell.

Lets say your script1.sh is :

a=10 

b=20

now run your script in your login shell :

Example:

.[space]script1

Now if you will access the variable a in your script it will be accessible to you without fail.

Example

Lets say your script2.sh is :

echo $a

run:

.[space]script2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文