首页  >  问答  >  正文

linux - 写一个修改文件中配置信息的脚本,执行错误

主要有三个文件 test.cnf test.sh test.txt
执行test.sh去读取test.cnf的配置来修改test.txt的内容,执行过程中读取配置成功但sed执行的时候没找到。
sed这里只是调试没去修改test.txt,只是显示test.txt的结果

[root@localhost /tmp]# head -100 test*
==> test.cnf <==
yy=123
ppp=456

==> test.sh <==
function myconf(){

source test.cnf
awk -F'=' '{print }' test.cnf|while read myline;do sed s/{{$myline}}/${$myline}/g test.txt;done
}
myconf

==> test.txt <==
uuu={{yy}}
ooo={{ppp}}

调试的时候就显示执行错误;

[root@localhost /tmp]# bash -x test.sh
+ myconf
+ source test.cnf
++ yy=123
++ ppp=456
+ read myline
+ awk -F= '{print }' test.cnf
test.sh: line 4: s/{{$myline}}/${$myline}/g: bad substitution
欧阳克欧阳克2663 天前775

全部回复(1)我来回复

  • 淡淡烟草味

    淡淡烟草味2017-07-04 13:47:27

     while read a b;do sed -n "s/$a/$b/p" test.txt;done < <(awk -F= '{print ,}' test.cnf)

    其它方法:

    awk -F= -vOFS='=' 'NR==FNR{a[]=;next}{for(i in a)if( ~ i)sub(i,a[i],)}1' test.cnf test.txt

    回复
    0
  • 取消回复