您的位置:首页技术文章
文章详情页

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

【字号: 日期:2024-06-12 18:28:21浏览:64作者:猪猪

问题描述

主要有三个文件 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=123ppp=456==> test.sh <==function myconf(){source test.cnfawk -F’=’ ’{print $1}’ 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 $1}’ test.cnftest.sh: line 4: s/{{$myline}}/${$myline}/g: bad substitution

问题解答

回答1:

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

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

其它方法:

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

相关文章: