在 Sh/Bash 和 php 中解析配置参数的最佳/最简单的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3480186/
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
Best/easiest way to parse configuration parameters in Sh/Bash and php
提问by Guillermo
I got in every php project (around 25!), some sh scripts that help me with routine tasks such as deployment, repo syncronizing, databases exporting/export, etc.
我参与了每个 php 项目(大约 25 个!),一些 sh 脚本可以帮助我完成日常任务,例如部署、repo 同步、数据库导出/导出等。
The sh scripts are the same for all the projects I manage, so there must be a configuration file to store diferent parameters that depend on the project:
我管理的所有项目的sh脚本都是一样的,所以必须有一个配置文件来存储依赖于项目的不同参数:
# example conf, the sintaxys only needs to be able to have comments and be easy to edit.
host=www.host.com
[email protected]
password=xxx
I just need to find a clean way in which this configuration file can be read (parsed) from a sh script, and at the same time, be able to read those same parameters from my PHP scripts. Without having to use XML.
我只需要找到一种干净的方式,可以从 sh 脚本读取(解析)此配置文件,同时能够从我的 PHP 脚本中读取相同的参数。无需使用 XML。
Do you know a good solution for this?
你知道一个好的解决方案吗?
Guillermo
吉列尔莫
采纳答案by Kaleb Pederson
If you don't want to source the file as pavanlimo showed, another option is to pull in the variables using a loop:
如果您不想像 pavanlimo 显示的那样获取文件,另一种选择是使用循环拉入变量:
while read propline ; do
# ignore comment lines
echo "$propline" | grep "^#" >/dev/null 2>&1 && continue
# if not empty, set the property using declare
[ ! -z "$propline" ] && declare $propline
done < /path/to/config/file
In PHP, the same basic concepts apply:
在 PHP 中,同样的基本概念适用:
// it's been a long time, but this is probably close to what you need
function isDeclaration($line) {
return $line[0] != '#' && strpos($line, "=");
}
$filename = "/path/to/config/file";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$lines = explode("\n", $contents); // assuming unix style
// since we're only interested in declarations, filter accordingly.
$decls = array_filter($lines, "isDeclaration");
// Now you can iterator over $decls exploding on "=" to see param/value
fclose($handle);
回答by pavanlimo
Simply source the script conf file as another sh file!.
只需将脚本 conf 文件作为另一个 sh 文件获取即可!
Example:
例子:
conf-file.sh:
conf-file.sh:
# A comment
host=www.host.com
[email protected]
password=xxx
Your actual script:
你的实际脚本:
#!/bin/sh
. ./conf-file.sh
echo $host $administrator_email $passwword
And the same conf-file can be parsed in PHP: http://php.net/manual/en/function.parse-ini-file.php
并且可以在 PHP 中解析相同的 conf 文件:http: //php.net/manual/en/function.parse-ini-file.php
回答by carlo
For a Bash INI file parser also see:
对于 Bash INI 文件解析器,请参见:
回答by ghostdog74
to parse the ini file from sh/bash
从 sh/bash 解析 ini 文件
#!/bin/bash
#bash 4
shopt -s extglob
while IFS="=" read -r key value
do
case "$key" in
!(#*) )
echo "key: $key, value: $value"
array["$key"]="$value"
;;
esac
done <"file"
echo php -r myscript.php ${array["host"]}
then from PHP, use argv
然后从 PHP,使用argv

