修改配置文件内容的shell

本文介绍了一个用于自动化更新配置文件的Shell脚本。该脚本能够精确地定位并替换指定配置项的值,确保配置更新过程的一致性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/sh

function updateConfigLine()
{
confFile=$1
lineKey=$2
lineValue=$3

if [ -z $1 -o -z $2 -o -z $3 ]; then
echo "parameters not match function:updateConfigLine()"
exit 1
fi

count=`grep $lineKey $confFile|wc -l|tr -d [BLANK]`
if [ $count -eq 0 ]; then
echo "not found $lineKey in file: $confFile"
exit 1
fi
if [ $count -gt 1 ]; then
echo "found more then one lines of $lineKey in file: $confFile"
exit 1
fi

sed "s/$lineKey.*=.*/$lineKey=$lineValue/g" $confFile > "$confFile"tmpforautoreplace
cat "$confFile"tmpforautoreplace > $confFile
rm -f "$confFile"tmpforautoreplace
echo "change to $lineKey=$lineValue in file $confFile successfully"
}

function help()
{
echo "run this shell with parameters like below."
echo "$0 filePathName itemKey itemvalue"
}

file=$1
key=$2
value=$3
validate=1

[ -z $1 ] && validate=0;
[ -z $2 ] && validate=0;
[ -z $3 ] && validate=0;

if [ $validate == 0 ]; then
echo "please input right parameters"
help
exit 1
fi

updateConfigLine $1 $2 $3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值