当前位置: 技术问答>linux和unix
替换函数求解
来源: 互联网 发布时间:2016-08-28
本文导语: 我想写个简单的替换函数。 替换的文件为database.ini 内容如下 [Database] Address="localhost" Login="root" Password="123" Database="nl_receive" Connections="10" EncryptPassword="no" 我的函数很简单,就向想把字段替换掉 我的函数为: vi sed.sh...
我想写个简单的替换函数。
替换的文件为database.ini 内容如下
[Database]
Address="localhost"
Login="root"
Password="123"
Database="nl_receive"
Connections="10"
EncryptPassword="no"
我的函数很简单,就向想把字段替换掉
我的函数为:
vi sed.sh
sed()
{
if [ $# -ge 3 ]
then
sed -i 's/$2/$3/g' $1
else
echo "you config errror! "
fi
}
[root@localhost ~]# ./sed.sh database.ini nl_receive aaa 运行的时候没把字段改过来,不知道是怎么回事
替换的文件为database.ini 内容如下
[Database]
Address="localhost"
Login="root"
Password="123"
Database="nl_receive"
Connections="10"
EncryptPassword="no"
我的函数很简单,就向想把字段替换掉
我的函数为:
vi sed.sh
sed()
{
if [ $# -ge 3 ]
then
sed -i 's/$2/$3/g' $1
else
echo "you config errror! "
fi
}
[root@localhost ~]# ./sed.sh database.ini nl_receive aaa 运行的时候没把字段改过来,不知道是怎么回事
|
#!/bin/sh
#add key=value
add()
{
addfile=$1
echo $queryfile
echo "Please input key and vlaue like key=value:"
while read add_key
do
#key_value is not null and can not be start with "#"
if [ -n "$add_key" ]
then
suffix=`echo "$add_key" | cut -d= -f2`
prefix=`echo "$add_key" | cut -d= -f1`
if [ ${add_key%=*} = "$prefix" -a ${add_key#*=} = "$suffix" ]
then
count=`cut -d= -f1 $addfile | grep -w $(echo $add_key | cut -d= -f1) | wc -l`
echo "count : $count"
if [ "$count" -gt 0 ]
then
echo "the $add_key is exist!"
echo "add $add_key to $addfile falid!"
break;
else
echo "$add_key" >> $addfile
echo "add $add_key to $addfile successful!"
break;
fi
else
echo "${add_key} format is error"
exit 3;
fi
fi
done
}
#update key=value
update()
{
updatefile=$1
echo "Please input key and vlaue like key=value:"
while read update_key
do
#key_value is not null and can not be start with "#"
if [ -n "$update_key" ]
then
prefix=`echo "$update_key" | cut -d= -f1`
# echo "prefix is $prefix"
suffix=`echo "$update_key" | cut -d= -f2`
# echo "suffix is $suffix"
prefix=`echo "$update_key" | cut -d= -f1`
# echo ${update_key%=*}
# echo ${update_key#*=}
if [ ${update_key%=*} = "$prefix" -a ${update_key#*=} = "$suffix" ]
then
count=`cut -d= -f1 $updatefile | grep -w $prefix | wc -l`
echo "count : $count"
if [ "$count" -gt 0 -a "$count" -eq 1 ]
then
sed 's/("$prefix")=[0-9a-zA-Z]*/1="${suffix}"/g' $updatefile$$
mv $updatefile$$ $updatefile
echo "Update $updatefile successful!"
break;
else
echo "$updatefile has no entry for $update_key or
too more entryin $updatefile!"
break;
fi
else
echo "${update_key} format is error"
break;
fi
fi
done
}
#del key=value
del()
{
delfile=$1
echo 'Please input the key for delete:'
while read query_key
do
if [ -n "$query_key" ]
then
echo "The query resut is:"
cat $delfile | grep -v -w $query_key > $delfile$$
mv $delfile$$ $delfile
echo "Delete $key successful!"
break;
fi
done
}
#query key=value
query()
{
queryfile=$1
echo $queryfile
echo 'Please input the key for query:'
while read query_key
do
if [ -n "$query_key" ]
then
echo "The query resut is:"
cut -d= -f1,2 $queryfile | grep -w $query_key
break;
else
echo "Please input the key for query:"
fi
done
}
#args check
check ()
{
file=$1
#check the operation file
if [ -f "$file" -a -e "$file" ]
then
:
else
echo "$file is a normal file or $file is not exsit !"
exit 2;
fi
#check the operation file's format,the format should be 'key=value'
while read key_value
do
#key_value is not null and can not be start with "#"
if [ -n "$key_value" -a "X${key_value%%#*}" != "X" ]
then
# echo "key_value is $key_value"
suffix=`echo "$key_value" | cut -d= -f2`
# echo "suffix is $suffix"
prefix=`echo "$key_value" | cut -d= -f1`
# echo "prefix is $prefix"
if [ ${key_value%=*} = "$prefix" -a ${key_value#*=} = "$suffix" ]
then
:
else
echo "${key_value} format is error"
exit 3;
fi
fi
done