如何使用 Bash 引用文件中的变量?
我想调用变量的设置文件。我怎样才能在 Bash 中做到这一点?
设置文件将定义变量(例如,CONFIG.FILE):
production="liveschool_joe"
playschool="playschool_joe"
并且脚本将在其中使用这些变量:
#!/bin/bash
production="/REFERENCE/TO/CONFIG.FILE"
playschool="/REFERENCE/TO/CONFIG.FILE"
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
I want to call a settings file for a variable. How can I do this in Bash?
The settings file will define the variables (for example, CONFIG.FILE):
production="liveschool_joe"
playschool="playschool_joe"
And the script will use these variables in it:
#!/bin/bash
production="/REFERENCE/TO/CONFIG.FILE"
playschool="/REFERENCE/TO/CONFIG.FILE"
sudo -u wwwrun svn up /srv/www/htdocs/$production
sudo -u wwwrun svn up /srv/www/htdocs/$playschool
How can I get Bash to do something like that? Will I have to use AWK, sed, etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简短的回答
使用
source
命令。使用
source
的示例例如:
config.sh
script.sh
请注意,此示例中
sh ./script.sh
的输出为:这是因为
source
命令实际运行程序。 config.sh 中的所有内容都会被执行。另一种方法
您可以使用内置的
export
命令,获取和设置“环境变量”也可以实现此目的。关于访问变量,您只需要了解运行
export
和echo $ENV
即可。访问环境变量的方式与访问局部变量的方式相同。要设置它们,请说:
在命令行。所有脚本都可以访问该值。
The short answer
Use the
source
command.An example using
source
For example:
config.sh
script.sh
Note that the output from
sh ./script.sh
in this example is:This is because the
source
command actually runs the program. Everything inconfig.sh
is executed.Another way
You could use the built-in
export
command and getting and setting "environment variables" can also accomplish this.Running
export
andecho $ENV
should be all you need to know about accessing variables. Accessing environment variables is done the same way as a local variable.To set them, say:
at the command line. All scripts will be able to access this value.
使用点甚至更短(采购):
Even shorter using the dot (sourcing):
使用
source
命令导入其他脚本:Use the
source
command to import other scripts:在 Bash 中,要获取某些命令的输出而不是文件:
参考
in Bash, to source some command's output, instead of a file:
reference
我遇到了同样的问题,特别是在安全方面,我在此处找到了解决方案。
我的问题是我想在 Bash 中编写一个部署脚本,其中的配置文件包含类似这样的路径。
现有的解决方案包括使用“SOURCE”命令并导入包含这些变量的配置文件。 'SOURCE path/to/file'
但是这个解决方案存在一些安全问题,因为源文件可以包含 Bash 脚本可以包含的任何内容。
这会产生安全问题。当您的脚本获取其配置文件时,恶意者可以“执行”任意代码。
想象一下这样的事情:
为了解决这个问题,我们可能只想允许该文件中采用
NAME=VALUE
形式的构造(变量赋值语法)以及注释(尽管从技术上讲,注释并不重要)。因此,我们可以使用相当于grep -E
的egrep
命令来检查配置文件。这就是我解决问题的方法。
I have the same problem specially in case of security and I found the solution here.
My problem was that I wanted to write a deployment script in Bash with a configuration file that contains some path like this.
An existing solution consists of use "SOURCE" command and import the configuration file with these variables. 'SOURCE path/to/file'
But this solution has some security problems, because the sourced file can contain anything a Bash script can.
That creates security issues. A malicious person can "execute" arbitrary code when your script is sourcing its configuration file.
Imagine something like this:
To solve this, we might want to allow only constructs in the form
NAME=VALUE
in that file (variable assignment syntax) and maybe comments (though technically, comments are unimportant). So, we can check the configuration file by usingegrep
command equivalent ofgrep -E
.This is how I have solve the issue.
将参数文件转换为环境变量
通常我会进行解析而不是采购,以避免文件中某些工件的复杂性。它还为我提供了专门处理报价和其他事情的方法。我的主要目标是将“=”后面的所有内容保留为文字,甚至包括双引号和空格。
请注意我必须做的一个小技巧,将引用的文本视为单个参数,并为我的
cntpars
函数留出空间。还需要进行一层额外的评估。如果我不这样做,如选项 2 所示,我将传递两个参数,如下所示:"value
content"
命令执行期间的双引号会导致双引号要保留的参数文件。因此第三个选项也失败了。
当然,另一种选择是不提供双引号中的变量,如选项 4 所示,然后确保在需要时引用它们。
只是要记住一些事情。
实时查找
我喜欢做的另一件事是进行实时查找,避免使用环境变量:
不是最有效的,但对于较小的文件工作得非常干净。
Converting a parameter file to environment variables
Usually I go about parsing instead of sourcing, to avoid complexities of certain artifacts in my file. It also offers me ways to specially handle quotes and other things. My main aim is to keep whatever comes after the '=' as a literal, even the double quotes and spaces.
Note the little trick I had to do to consider my quoted text as a single parameter with space to my
cntpars
function. There was one extra level of evaluation required. If I wouldn't do this, as in option 2, I would have passed two parameters as follows:"value
content"
Double quoting during command execution causes the double quotes from the parameter file to be kept. Hence the 3rd Option also fails.
The other option would be of course to just simply not provide variables in double quotes, as in option 4, and then just to make sure that you quote them when needed.
Just something to keep in mind.
Real-time lookup
Another thing I like to do is to do a real-time lookup, avoiding the use of environment variables:
Not the most efficient, but with smaller files works very cleanly.
如果正在生成变量但未将其保存到文件中,则无法将它们通过管道传送到
source
中。看似简单的方法是这样的:If the variables are being generated and not saved to a file you cannot pipe them in into
source
. The deceptively simple way to do it is this:为了防止命名冲突,仅导入您需要的变量:
For preventing naming conflicts, only import the variables that you need:
包含变量的脚本可以使用 Bash 导入执行。
考虑 script-variable.sh 文件:
考虑将使用变量的实际脚本:
The script containing variables can be executed imported using Bash.
Consider the script-variable.sh file:
Consider the actual script where the variable will be used: