修改設定檔

來源:互聯網
上載者:User

系統營運時有時候需要用指令碼來修改設定檔

newlisp下如何修改檔案呢。下面有個簡單的例子,來自別人回答我的問題:

(set 'input-file {/tmp/temp.txt})(set 'lines (parse (read-file input-file)  "\n"))(replace "ENGINE_HOME=" lines "ENGINE_HOME=/opt/engine_home")(write-file input-file (join lines "\n")) 

上面的例子是把/tmp/temp.txt檔案裡的內容讀出來,然後用replace進行文本替換,

"ENGINE_HOME"全部替換成"ENGINE_HOME=/opt/engine_home"

不錯,非常方便。但是並不是什麼時候都能用。

有時候replace函數就是找不到lines裡面的字串,原因不明。因此我有寫了一個規矩的函數,遍曆每一行,然後replace,再寫迴文件。

  (set 'nginx-conf-file (append (env "ENGINE_HOME") "/nginx/conf/carrier.conf"))  (set 'lines (parse (read-file nginx-conf-file)  "\n"))  (set 's (length lines))  (set 'i 0)  (do-until (= i s)    (begin      (replace "ENGINE_HOME" (lines i) (env "ENGINE_HOME"))      (inc i)      ))  (write-file nginx-conf-file (join lines "\n"))

這個例子是用來修改nginx設定檔的,將佔位符ENGINE_HOME全部替換成環境變數裡面的值。

使用了一個do-until進行遍曆。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.