你好,游客 登录
背景:
阅读新闻

修改配置文件 - sheismylife的专栏

[日期:2013-03-20] 来源:  作者: [字体: ]

系统运维时有时候需要用脚本来修改配置文件

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进行遍历。






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻