您的位置:首页 > 运维架构 > Shell

修改配置文件内容的shell

2012-03-24 20:15 260 查看
#!/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: