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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6770450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:10:19  来源:igfitidea点击:

Passing one shell script variable to another shell script

linuxshellshexternal

提问by Karthik

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 script1in script2but its executing all the commandswritten in script1.shwhich is annoying. I just want to access only the exported variables in script1and donot want to run the commands in script1while calling that in script2.

这里涉及的问题是,它能够从访问变量a SCRIPT1SCRIPT2但其执行的所有命令写在script1.sh这是烦人。我只是想访问仅在导出的变量SCRIPT1和DONOT想在运行命令SCRIPT1同时呼吁,在SCRIPT2

Kindly help!

请帮助!

Thanks,
Karthik

谢谢,
卡西克

回答by Ignacio Vazquez-Abrams

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 解析器,否则无法完成您想要的操作。

回答by c00kiemon5ter

I agree with Ignacio Vazquez-Abrams.

我同意 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 configurationscript. So both script1and script2would sourcethat one, to use the vars defined there.

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

回答by evil otto

If your variable assignments are only everin the exactform indicated above, that is

如果您的变量分配只是上面指出的确切形式,那就是

export a=10

then you could extract those assignments with grepand evalthe results. Your script2.sh would then be

然后你可以提取这些作业grepeval结果。你的 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.

这些都是潜在的问题。

回答by jlliagre

script2.sh:

脚本2.sh:

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

回答by lesmana

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

回答by Vimukt

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

回答by Peter

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 需要检查传递的参数是否为空。