配置文件pro.properties内容如下:
#参数列表
city=beijing,shangh
name="zhang san"
Shell脚本test_pro.sh如下:
#!/bin/bash
OIFS=$IFS
source ~/.bash_profile
#程序根目录
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
#加载主配置文件
source ${baseDirForScriptSelf}/pro.properties
#输出参数
echo ${city}
echo ${name}
执行shell输出:
[root@liand ~]# sh test_pro.sh
beijing,shangh
zhang san
注意:
1、编写shell和配置文件的时候一定注意文件编码为unix和utf-8的格式,如下:
否则在运行脚本的时候会报莫名其妙的错误
2、如果配置文件里面的参数有空格,如name=“zhang san”,一定要把zhang san用双引号引起来,不然会报如下错误:
[root@liand ~]# sh test_pro.sh
/usr/project/pro.properties: line 3: san: command not found
beijing,shangh