有三個配置文件conf1.properties、conf2.properties、conf3.properties,和一個標準文件right.properties。標準文件中的內容爲三個配置文件中所有的配置項及標準值。
三個配置文件在/home/conf目錄下,標準文件在/home下.
conf1.properties>>>>>>>>>>
user.name=zhangsan
user.number=123456
...
conf2.properties>>>>>>>>>>
service.IP=127.0.0.1
user.job=SE
...
conf3.properties>>>>>>>>>>
woker.hobby=soccer
woker.selary=2000
...
right.properties>>>>>>>>>>
name=zhangsan
number=123456
...
IP=127.0.0.1
job=SE
...
hobby=soccer
selary=2000
...
要求:運行test腳本,將三個文件中的配置項的值改爲標準配置文件中對應配置項的值。
#!/bin/bash
current_dir='/home'
CONF_DIR=${current_dir}/conf
Install_Param_file=${current_dir}/right.properties
##批量修改linux文件格式
sudo find ${current_dir}/conf -name "*.properties" | xargs dos2unix -q
sudo find ${current_dir} -name "*.properties" | xargs dos2unix -q
sudo find ${current_dir} -name "*.sh" | xargs dos2unix -q
LOCAL_IP=$(ifconfig eth0|grep -w inet | awk -F: '{print $2}' | awk '{print $1}')
HOST_NAME=$(hostname)
##根據參數獲取配置參數並配置參數
function getParameter()
{
Par=$(cat ${Install_Param_file} | grep "^$1" |awk -F '=' '{print $2}')
sed -i "s#$1=.*#$1=$Par#g" $2
}
##修改一些配置
function updateVODConfig()
{
getParameter user.name ${CONF_DIR}/mongo.properties
getParameter user.number ${CONF_DIR}/service.properties
getParameter service.IP ${CONF_DIR}/service.properties
getParameter user.job ${CONF_DIR}/service.properties
getParameter woker.hobby ${CONF_DIR}/service.properties
getParameter woker.selary ${CONF_DIR}/service.properties
}
function main()
{
echo ${LOCAL_IP}
echo ${HOST_NAME}
updateVODConfig
}
main
echo "update successfully"
exit 0
shell中更新多個配置文件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.