简体   繁体   English

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

[英]Passing one shell script variable to another shell script

I am trying to access one script variable from another script for example,例如,我正在尝试从另一个脚本访问一个脚本变量,

script1.sh:脚本1.sh:

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

script2.sh:脚本2.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.这里涉及的问题是它能够从script2中的script1访问变量“a”,但它执行所有script1.sh编写的命令,这很烦人。 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 .我只想访问script1中的导出变量,并且不想在script2中调用时在script1中运行命令。

Kindly help!请帮忙!

Thanks,谢谢,
Karthik卡尔提克

Assigning a variable is a command.分配变量是一个命令。 Unless you're prepared to write a bash parser in bash, what you want cannot be done.除非您准备在 bash 中编写 bash 解析器,否则您想要的东西是做不到的。

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:脚本1.sh:

. common-vars
echo "testing"
exit

script2.sh:脚本2.sh:

. common-vars
echo $a

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.然后您可以使用grep提取这些分配并eval结果。 Your script2.sh would then be您的 script2.sh 将是

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.这些都是潜在的问题。

I agree with "Ignacio Vazquez-Abrams" this is a good answer to your question.我同意“Ignacio Vazquez-Abrams”的观点,这是对您问题的一个很好的回答。

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.概念是基于:使用你登录shell来运行这两个脚本而不是一个新的shell。

Lets say your script1.sh is:假设您的 script1.sh 是:

a=10 

b=20

now run your script in your login shell:现在在您的登录 shell 中运行您的脚本:

Example:例子:

.[space]script1

Now if you will access the variable a in your script it will be accessible to you without fail.现在,如果您将访问脚本中的变量 a ,您将可以访问它而不会失败。

Example例子

Lets say your script2.sh is:假设您的 script2.sh 是:

echo $a

run:跑:

.[space]script2

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.这当然意味着脚本 B 将被脚本 A 调用。脚本 B 需要检查传递的参数是否为空。

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.所以script1script2都将获取那个source ,以使用那里定义的变量。

script2.sh:脚本2.sh:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM