the*_*078 7 variables bash file-io
我有一个包含以下内容的配置文件:
msgs.config:
tmsg:This is Title Message!
t1msg:This is T1Message.    
t2msg:This is T2Message.    
pmsg:This is personal Message!
我正在编写一个bash脚本,它读取msgs.config文件变量并将它们存储到局部变量中.我将在整个脚本中使用这些.由于许可,我不想使用该.方法(来源).
tmsg
t1msg
t2msg
pmsg
任何帮助将不胜感激.
您可以使用:
oldIFS="$IFS"
IFS=":"
while read name value
do
    # Check value for sanity?  Name too?
    eval $name="$value"
done < $config_file
IFS="$oldIFS"
或者,您可以使用关联数组:
declare -A keys
oldIFS="$IFS"
IFS=":"
while read name value
do
    keys[$name]="$value"
done < $config_file
IFS="$oldIFS"
现在您可以参考${keys[tmsg]}etc来访问变量.或者,如果变量列表是固定的,您可以将值映射到变量:
tmsg="${keys[tmsg]}"