将一个 shell 脚本变量传递给另一个 shell 脚本
我正在尝试从另一个脚本访问一个脚本变量,例如
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您尝试执行的操作(仅从另一个脚本获取变量,而不执行命令)是不可能的。
这是一个解决方法:
将变量声明和初始化提取到第三个文件,然后由两个文件获取该文件。
common-vars:
script1.sh:
script2.sh:
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:
script1.sh:
script2.sh:
如果您的变量赋值仅采用上述精确形式,那么
您可以使用
grep
和eval提取这些赋值
结果。你的 script2.sh 将会是但是这样做非常脆弱,并且可能会以多种方式破坏。正如其他人所建议的那样,将变量分配放入由双方提供的第三个脚本中要安全得多。
如果您在条件语句中有变量设置,或者您导出的变量依赖于其他丢失的非导出变量,或者您单独设置和导出变量,则这可能会中断;如果您碰巧在导出中使用命令替换,那么它们仍然会运行,并可能产生副作用。
这些都是潜在的问题。
If your variable assignments are only ever in the exact form indicated above, that is
then you could extract those assignments with
grep
andeval
the results. Your script2.sh would then beBut 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.
These are all potentially problematic.
分配变量是一个命令。除非你准备在 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.
script2.sh:
script2.sh:
我同意伊格纳西奥·巴斯克斯·艾布拉姆斯的观点。
您对此无能为力。
一个可能的解决方案是将变量定义分离到第三个脚本,该脚本将用作配置脚本。因此,script1 和 script2 都会
source
那个脚本,以使用那里定义的变量。I agree with Ignacio Vazquez-Abrams.
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.您可以通过将变量作为参数传递来将变量从一个脚本传递到另一个脚本。
这当然意味着脚本 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.
我同意“Ignacio Vazquez-Abrams”的观点,这是对你的问题的一个很好的答案。
但我为您提供了另一种解决方案,您无需导出变量。概念基于:使用您的登录 shell 来运行脚本而不是新的 shell。
假设您的 script1.sh 是:
现在在登录 shell 中运行您的脚本:
示例:
现在,如果您将访问脚本中的变量 a,您将可以访问它,而不会失败。
示例
假设您的 script2.sh 是:
运行:
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 :
now run your script in your login shell :
Example:
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 :
run: