版权声明:Summer https://blog.csdn.net/csdnyanglei/article/details/82313803
#!/bin/bash
#author:summer
#file name: configSetting.sh
#function: set config info
config_path="./conf/watch_config.conf"
valid_paras=("ip","port","ip2","port2","test_ip","test_port")
#提醒信息
warn_tips(){
echo "==>Tips:$1"
echo
}
#校验参数是否被允许执行
check_para_valid(){
if [[ "${valid_paras[@]/$1/}" = "${valid_paras[@]}" ]] ;then
warn_tips 'error:invalid order'
exit 1
fi
}
#是否仅匹配一行,不止一行则退出
matched_one_row(){
# warn_tips "matched_one_row execute,\$1:$1 "
check_para_valid $1
if [ `grep -c "^\s*$1=" $config_path` -eq 1 ] ;then
# warn_tips 'it is just 1 row matched'
return 0
else
warn_tips 'it is more one row matched'
exit 1
fi
}
replace_matched_row(){
# warn_tips "replace matched row,\$1:$1 , \$2:$2"
matched_one_row $1
sed -i "s/^\s*$1=.*$/$1=$2/g" $config_path
}
search_para_value(){
#校验是否仅有一条记录
matched_one_row $1
grep "^\s*$1=" $config_path
}
#warn_tips 'hello,user!'
#入参校验
if [ $# -eq 0 ] ;then
warn_tips "you can search or reset config paras,just like operating git config process,\$\#:$#"
exit 1
elif [ $# -eq 1 ] ;then
# warn_tips 'config search'
search_para_value $1
elif [ $# -eq 2 ] ;then
# warn_tips 'config setting'
replace_matched_row $1 $2
fi