linux shell 读取配置文件的一个例子

配置文件内容如下:

#Created by JInto - www.guh-software.de
#Tue Aug 28 22:20:17 GMT+08:00 2018
aas_amountuser_switch=1
aas_fleet_switch=1
aas_usertokenproct_switch=1
aas_usertokenproct_num=5
aas_devtokenproct_switch=1
......

读取脚本如下:

# /bin/bash

configuration_file_path="/opt/some_path/common-conf"

current_value=`grep 'aas_amountuser_switch' ${configuration_file_path}/uiConfig.properties | cut -d = -f2  | sed 's/\r//'`
echo "$current_value"

if [ "$current_value" = "0" ]; then
  sed -i 's/aas_amountuser_switch=0/aas_amountuser_switch=1/' ${configuration_file_path}/uiConfig.properties
  echo "modify uiConfig.properties.aas_amountuser_switch success"
else
  echo "modify uiConfig.properties.aas_amountuser_switch error"
  exit
fi

注意:

  linux 中文件的末尾有字符 '\r',需要注意将换行符去掉

  这种小的点不太容易发现,所以看脚本看不出问题的时候,可以打开 shell 的 debug,很容易定位出来,如下:

    sh -x test.sh

猜你喜欢

转载自www.cnblogs.com/SBJBA/p/11761489.html