替换函数求解

blueskysee 2010-05-12 11:27:47
我想写个简单的替换函数。
替换的文件为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 运行的时候没把字段改过来,不知道是怎么回事

...全文
97 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueskysee 2010-05-21
  • 打赏
  • 举报
回复
恩,大体已经有那个意思了
yangyinbo 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 blueskysee 的回复:]

主席,脚本之前已经弄好了,我是想写个函数,然后传递参数给我写的函数。

如果直接用脚本的话,就不是我的目的了。

启用关键字是我的失误,本来我是test.sh
[/Quote]


#!/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 > $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 < $file
}

#main menu

#check the operation file
if [ "$#" = 0 ]
then
echo "Usage: keytool file"
exit 1;
fi

operationfile=$1
check $operationfile

while true
do
echo '
Would you like to :
1.Add an entry as key=value
2.Update an entry as key=value
3.Del an entry by key
4.Query an entry by key
5.Exit this progrom

Please select one of above (1-5): '
read choice
case "$choice" in
1)add $operationfile;;
2)update $operationfile;;
3)del $operationfile;;
4)query $operationfile;;
5)exit 0;;
*)echo "Bad choice!"
esac



done



那天自己写了个对key value进行操作的小脚本,执行试试
steptodream 2010-05-12
  • 打赏
  • 举报
回复

sed()
{
if [ $# -ge 3 ]
then
sed -i 's/$2/$3/g' $1
else
echo "you config errror! "
fi
}

这就是你的脚本内容吗 你把处理写成函数sed() 可是你根本没调用啊 而且你的函数名还用关键字
而且sed里用shell变量也不对
修改如下

#!/bin/bash
if [ $# -ge 3 ]
then
sed -i -e 's/'"$2"'/'"$3"'/g' $1
else
echo "you config errror! "
fi
blueskysee 2010-05-12
  • 打赏
  • 举报
回复
主席,脚本之前已经弄好了,我是想写个函数,然后传递参数给我写的函数。

如果直接用脚本的话,就不是我的目的了。

启用关键字是我的失误,本来我是test.sh

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧