linux - Write a script to modify the configuration information in the file, execution error
欧阳克
欧阳克 2017-07-04 13:45:27
0
1
777

There are three main files test.cnf test.sh test.txt
Execute test.sh to read the configuration of test.cnf to modify the content of test.txt. During the execution, the configuration is read successfully but sed is executed. The time was not found.
sed is just debugging here without modifying test.txt, it just displays the results of 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}}

Execution errors are displayed during debugging;

[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

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

Other methods:

awk -F= -vOFS='=' 'NR==FNR{a[]=;next}{for(i in a)if( ~ i)sub(i,a[i],)}1' test.cnf test.txt
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!