通常sed指令修改行内容时使用:sed -i " 9 s/^.*/"type in what you want modified!"/"
其中"type in what you want modified!"为替换后希望得到的内容。
由于用作替换的字符串里面不包括与sed替换格式相同的"/",因此可以正常使用。
But,当希望通过sed方式修改配置文件中的目录时,由于用作替换的目录字符换(datapath=/home/datatest/)中包含"/",会影响sed的作用。
However,可用通过一下sed方式做替换操作:sed -i " 9 s:^.*:${datapath}:"
即:把"/"替换为":"
BTW,sed中使用“”比使用‘’明智。
自测示例脚本run.sh如下:
#!/bin/bash
#脚本名字run.sh,用于修改配置文件中数据的源路径和目标路径
getdatapath(){
# echo "入参:"$*
if [ $# -eq 3 ];then
if [[ ! -z $1 &&
! -z $2 &&
! -z $3 ]];then
local emstype="$1"
local srcdir="$2"
local dstdir="$3"
local configfile="../conf/DataParse.ini"
#获取对应配置文件中需要修改的行数
local srclinenum=$(grep -n "SrcPath=" ${configfile}|awk -F ':' '{print $1}')
#对指定行所有内容进行重写
sed -i ""${srclinenum}" s:^.*$:SrcPath=${srcdir}:" $configfile
local dstlinenum=$(grep -n "ResultPath=" ${configfile}|awk -F ':' '{print $1}')
sed -i ""${dstlinenum}" s:^.*$:ResultPath=${dstdir}:" $configfile
else
echo "run.sh--ERROR : Some parameter is null!"
fi
else
echo "run.sh--ERROR : The parameter number is not matched!"
fi
}
#获取源数据目录和目标目录
#getdatapath $*
getdatapath "testtype" "/home/srcdata/" "/home/dstdata/"