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=123ppp=456==> test.sh <==function myconf(){source test.cnfawk -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.cnftest.sh: line 4: s/{{$myline}}/${$myline}/g: bad substitution
問題解答
回答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
相關文章:
