bash OpenVPN 源变量不适用于 debian
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23389655/
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
OpenVPN Source vars not working on debian
提问by il0venoobs
I have to create a script to setup an OpenVPN server automatically. In this script I need to source the vars file in /etc/openvpn/easy-rsa/
我必须创建一个脚本来自动设置 OpenVPN 服务器。在这个脚本中,我需要在 /etc/openvpn/easy-rsa/ 中获取 vars 文件
But when I'm executing the following script in the /etc/openvpn/easy-rsa/ folder (with chmod 775 on the script and vars file) it says "xxxx.sh: 3: xxxx.sh: source: not found:"
但是,当我在 /etc/openvpn/easy-rsa/ 文件夹(脚本和 vars 文件上带有 chmod 775)中执行以下脚本时,它说“xxxx.sh: 3: xxxx.sh: source: not found: ”
#!/bin/bash
source ./vars
When I write . ./vars, it works, but then if I want to do a ./clean-all it says :
当我写 . ./vars,它可以工作,但是如果我想做一个 ./clean-all 它说:
Please source the vars script first (i.e. "source ./vars") Make sure you have edited it to reflect your configuration.
请首先获取 vars 脚本(即“source ./vars”) 确保您已编辑它以反映您的配置。
When I do the ./clean-all in the same script than the . ./vars, it works.
当我在与 . ./vars,它有效。
Thanks for your help (and sorry for my bad english :/)
感谢您的帮助(并为我的英语不好:/)
采纳答案by Josh Jolly
When you source
(or .
) a file, all the commands inside it are read and executed - this includes variable assignments. However, when a variable assignment takes place, it takes place only for the current shell. When you run a script, a subshell is created - so any variables inside the script are only visible within the subshell, not the parent (calling) shell. This is why it works when you have run source
and clean-all
within the same script, it should also work if you do both from the command line, ie:
当你source
(或.
)一个文件时,它里面的所有命令都会被读取和执行——这包括变量赋值。然而,当变量赋值发生时,它只发生在当前 shell 中。当您运行脚本时,会创建一个子外壳 - 因此脚本内的任何变量仅在子外壳中可见,而不是父(调用)外壳。这就是为什么它在您运行source
并clean-all
在同一个脚本中工作时,如果您从命令行执行这两项操作,它也应该工作,即:
$ . /etc/openvpn/easy-rsa/vars
$ /etc/openvpn/easy-rsa/clean-all