linux - 写一个修改文件中配置信息的脚本,执行错误
欧阳克
欧阳克 2017-07-04 13:45:27
0
1
755

主要有三个文件 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
欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

全部回复(1)
淡淡烟草味
 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!