Answers:
正如mbiber所说的,source
另一个文件。例如,您的配置文件(例如some.config
)将是:
var1=val1
var2=val2
您的脚本可能如下所示:
#! /bin/bash
# Optionally, set default values
# var1="default value for var1"
# var1="default value for var2"
. /path/to/some.config
echo "$var1" "$var2"
/etc/default
通常,许多文件以类似的方式用作其他Shell脚本的配置文件。这里的帖子中一个非常常见的示例是/etc/default/grub
。该文件用于设置GRUB的配置选项,因为它grub-mkconfig
是提供该文件的Shell脚本:
sysconfdir="/etc"
#…
if test -f ${sysconfdir}/default/grub ; then
. ${sysconfdir}/default/grub
fi
如果您确实必须处理表单的配置:
var1 some value 1
var2 some value 2
然后,您可以执行以下操作:
while read var value
do
export "$var"="$value"
done < /path/to/some.config
(您也可以执行类似的操作eval "$var=$value"
,但这比采购脚本要冒险。与源文件相比,您可能无意间破坏了该文件。)
=
从一开始就不能编写它?
/etc/default
- 的各种文件中使用shell代码-并充分利用。如果您的笨拙用户可以访问以root身份运行的某些东西的配置,那么这已经是一个失败的原因。
显然,我不是bash
这里的专家,但是无论您使用哪种语言,概念都不应有所不同:
在下面的示例中,您可以使用(非常)基本的脚本来按照配置文件中的设置设置字符串或打印字符串:
#!/bin/bash
# argument to set a new string or print the set string
arg=$1
# possible string as second argument
string=$2
# path to your config file
configfile="$HOME/stringfile"
# if the argument is: "set", write the string (second argument) to a file
if [ "$arg" == "set" ]
then
echo "$string" > $configfile
# if the argunment is "print": print out the set string, as defined in your file
elif [ "$arg" == "print" ]
then
echo "$( cat $configfile )"
fi
要将字符串设置到您的配置文件中:
$ '/home/jacob/Bureaublad/test.sh' set "Een aap op een fiets, hoe vind je zoiets?"
随后,按照“ configfile”中的定义打印出字符串:
$ '/home/jacob/Bureaublad/test.sh' print
Een aap op een fiets, hoe vind je zoiets?
当然,在实际应用的脚本中,您需要添加很多内容以确保参数正确,确定输入不正确,设置文件不存在时的处理方法等,但是:
这是基本思路
我正在使用这个...
#!/bin/bash
CFG_FILE=/etc/test.conf
CFG_CONTENT=$(cat $CFG_FILE | sed -r '/[^=]+=[^=]+/!d' | sed -r 's/\s+=\s/=/g')
eval "$CFG_CONTENT"
Conf文件用sed解析,然后评估为简单变量赋值
sed首先解析键值(也支持=包含空格)
第二个sed删除=号周围的空格,以表示有效的变量分配
可以添加进一步的治疗
conf文件中所有其他不匹配的文本将被删除(包括#或;注释和其他)
请注意,也可以从此配置文件评估单行外壳命令!
sed
命令将为问题中的示例配置文件条目生成一个空输出。-1
.
命令和进程重定向而不是临时变量来保存子进程的输出:. <(sed ... "$CFG_FILE")
eval $(sed -r '/[^=]+=[^=]+/!d;s/\s+=\s/=/g' "$CFG_FILE")
,这样可以更好地了解正在发生的情况。
.
期望将文件和进程替换扩展为文件。. <(echo echo foo)
可以按照Bash v4.3.11中的预期工作。在较小的示例中,这可能无关紧要。我宁愿不要通过命令替换传递多个千字节。
#------------------------------------------------------------------------------
# parse the ini like $0.$host_name.cnf and set the variables
# cleans the unneeded during after run-time stuff. Note the MainSection
# courtesy of : http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
#------------------------------------------------------------------------------
doParseConfFile(){
# set a default cnfiguration file
cnf_file="$run_unit_bash_dir/$run_unit.cnf"
# however if there is a host dependant cnf file override it
test -f "$run_unit_bash_dir/$run_unit.$host_name.cnf" \
&& cnf_file="$run_unit_bash_dir/$run_unit.$host_name.cnf"
# yet finally override if passed as argument to this function
# if the the ini file is not passed define the default host independant ini file
test -z "$1" || cnf_file=$1;shift 1;
test -z "$2" || ini_section=$2;shift 1;
doLog "DEBUG read configuration file : $cnf_file"
doLog "INFO read [$ini_section] section from config file"
# debug echo "@doParseConfFile cnf_file:: $cnf_file"
# coud be later on parametrized ...
test -z "$ini_section" && ini_section='MAIN_SETTINGS'
doLog "DEBUG reading: the following configuration file"
doLog "DEBUG ""$cnf_file"
( set -o posix ; set ) | sort >"$tmp_dir/vars.before"
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/#.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
< $cnf_file \
| sed -n -e "/^\[$ini_section\]/,/^\s*\[/{/^[^#].*\=.*/p;}"`
( set -o posix ; set ) | sort >"$tmp_dir/vars.after"
doLog "INFO added the following vars from section: [$ini_section]"
cmd="$(comm -3 $tmp_dir/vars.before $tmp_dir/vars.after | perl -ne 's#\s+##g;print "\n $_ "' )"
echo -e "$cmd"
echo -e "$cmd" >> $log_file
echo -e "\n\n"
sleep 1; printf "\033[2J";printf "\033[0;0H" # and clear the screen
}
#eof func doParseConfFile