linux - 写一个修改文件中配置信息的脚本,执行错误
欧阳克
欧阳克 2017-07-04 13:45:27
[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 $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.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 $1,$2}' test.cnf)

其它方法:

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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号