linux-脚本-配置文件的工具

#!/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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值