首頁  >  問答  >  主體

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 }' 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
欧阳克欧阳克2663 天前773

全部回覆(1)我來回復

  • 淡淡烟草味

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

    其它方法:

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

    回覆
    0
  • 取消回覆