search

Home  >  Q&A  >  body text

linux - Write a script to modify the configuration information in the file, execution error

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
欧阳克欧阳克2743 days ago835

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味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)

    Other methods:

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

    reply
    0
  • Cancelreply