在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
在Linux下如何用Shell脚本读写XML?现有一个config.xml
<?xml version="1.0" encoding="UTF-8"?> <config> <server-ip>192.168.1.45</server-ip> <server-port>1209</server-port> <repository-temp-path>/home/john</repository-temp-path> </config>
需要修改里面的"server-ip", "server-port" and "import-path",用Shell脚本的参数$1,$2,$3来写入。
思路1:用sed实现
首先想到的就是用sed正则匹配替换实现,写了一个shell脚本,是这样的:
#!/bin/sh if [ $# -ne 3 ];then echo "usage: argument 1:IP_Address 2:Server_PORT 3:Temp_PATH" exit 1 fi IP=$1 PORT=$2 DIRT=$3 echo "Change values in config.xml..." sed "s/<server-ip>.*<\/server-ip>/<server-ip>${IP}<\/server-ip>/;s/<server-port>.*<\/server-port>/<server-port>${PORT}<\/server-port>/;s/<repository-temp-path>.*<\/repository-temp-path>/<repository-temp-path>${DIRT}<\/repository-temp-path>/" config.xml > config.xml echo "Done."
测试下来调用$ ./abc.sh 192.168.1.6 9909 \\/home\\/abc"是可以的,但环境变量不行,例如:$ ./abc.sh 192.168.1.6 9909 $HOME\\/abc",因为首先环境变量被解析了,所以存在反斜杠转义字符和sed替换冲突的问题。
用另外一个思路实现
另外一个思路是直接输出该xml的内容,测试下来很管用,使用很方便,不存在反斜杠转义字符的问题和环境变量的问题:
#!/bin/sh if [ $# -ne 3 ];then echo "usage: argument 1:IP_Address 2:Server_PORT 3:Temp_PATH" exit 1 fi IP=$1 PORT=$2 DIRT=$3 echo "Change values in config.xml..." cat <<EOF >config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <server-ip>${IP}</server-ip> <server-port>${PORT}</server-port> <repository-temp-path>${DIRT}</repository-temp-path> </config> EOF echo "Done."
思路3:用XMLStarlet
$ xmlstarlet ed -u /config/server-ip -v 192.168.1.6 -u /config/server-port -v 9909 -u /config/repository-temp-path -v /home/bbb input.xml <?xml version="1.0" encoding="UTF-8"?> <config> <server-ip>192.168.1.6</server-ip> <server-port>9909</server-port> <repository-temp-path>/home/bbb</repository-temp-path> </config>
思路4:用xsltproc
很多Linux比如CentOS默认已安装xsltproc,所以用xslt可以很方便的把一个xml转换为另外一个xml。具体用法见这个网页。
在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)的更多相关文章
- linux 下RMAN备份shell脚本
RMAN备份对于Oracle数据库的备份与恢复简单易用,成本低廉.对于使用非catalog方式而言,将RMAN脚本嵌入到shell脚本,然后再通过crontab来实现中小型数据库数据库备份无疑是首选. ...
- Linux下如何执行Shell脚本
Linux下你可以有两种方式执行Shell脚本: 1.用shell程序执行脚本:根据你的shell脚本的类型,选择shell程序,常用的有sh,bash,tcsh等(一般来说第一行#!/bin/bas ...
- linux下如何编写shell脚本
我对shell脚本的认识,除了执行过同事写的shell 脚本外,其他一无所知,为了让自己强大,我决定自己研究shell脚本,也许在你看来很简答,没必要说这么多废话,但是我希望在我的技术log里记录下来 ...
- linux下后台执行shell脚本nohup
(一)使用nohup后台执行脚本 脚本执行结果记录到nohup.out文件中 (二)使用&后台执行脚本 使用&符号在后台执行命令或脚本后,如果你退出登录,这个命令就会被自动终止掉
- Linux Shell脚本读写XML文件
在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...
- [转]Linux下的lds链接脚本详解
转载自:http://linux.chinaunix.net/techdoc/beginner/2009/08/12/1129972.shtml 一. 概论 每一个链接过程都由链接脚本(lin ...
- 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记
第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...
- Linux下的lds链接脚本简介
转载:http://hubingforever.blog.163.com/blog/static/171040579201192472552886/ 一. 概论 每一个链接过程都由链接脚本(lin ...
- linux的基本操作(shell 脚本的基础知识)
shell 脚本的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本 ...
随机推荐
- vim 多窗口编辑
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- 开发板怎样开启telnet服务
linux开发板开启telnet服务须要一下几个条件: 1.文件系统支持telnet busybox默认是把telnet和telnetd功能编进去了的,所以这一步一般都省了. 2.挂载devpts 挂 ...
- RadioButtonList选择事件onclick
<asp:RadioButtonList ID="rbtnFInvoiceType" runat="server" onclick="check ...
- C# 过滤特殊字符
. /// <summary> /// 过滤不安全的字符串 /// </summary> /// <param name="Str"></ ...
- CentOS上编译安装Git
1. 安装(编译安装)软件 # 先安装git依赖的包 yum install zlib-devel yum install openssl-devel yum install perl yum ins ...
- 轻松搞定javascript原型链 _proto_
//如有错误或不同观点,欢迎批评与讨论! 首先,prototype出现的目的,是为了解决 代码重用 的问题 , prototype 相当于是在内存上划分出一个公共的区域, 专用于存放 实例化对象 的相 ...
- Get a handle on PHP Handlers
PHP Handlers? mod_php? FPM? How do we make sense of the inner workings of PHP outside of our lines o ...
- Selenium2Library使用Remote功能(转载并更新)
在selenium2library库的open browser中,除了我们常用的url,browser外,还有几个不常用的参数.如:remote_url的用法 1.下载selenium-server- ...
- 异步操作AsycnTask类
1. 首先执行onPreExecute方法,进行UI的初步设置 2. 其次执行doInBackground方法,此时将不在UI中线程中进行了 3. 然后如果要进行中的数据的话可以通过publis ...
- VC操作Image的三种方法(收集)
忘记从哪来收集过来的资料了,暂且不管是哪位老兄写的,只道一声谢谢. 仅管VC有提供相应的API和类来操作bmp位图.图标和(增强)元文件,但却不支持jpg.gif和png等格式的图片,而这几种格式却是 ...