在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

XML + shell = 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(转)的更多相关文章

  1. linux 下RMAN备份shell脚本

    RMAN备份对于Oracle数据库的备份与恢复简单易用,成本低廉.对于使用非catalog方式而言,将RMAN脚本嵌入到shell脚本,然后再通过crontab来实现中小型数据库数据库备份无疑是首选. ...

  2. Linux下如何执行Shell脚本

    Linux下你可以有两种方式执行Shell脚本: 1.用shell程序执行脚本:根据你的shell脚本的类型,选择shell程序,常用的有sh,bash,tcsh等(一般来说第一行#!/bin/bas ...

  3. linux下如何编写shell脚本

    我对shell脚本的认识,除了执行过同事写的shell 脚本外,其他一无所知,为了让自己强大,我决定自己研究shell脚本,也许在你看来很简答,没必要说这么多废话,但是我希望在我的技术log里记录下来 ...

  4. linux下后台执行shell脚本nohup

    (一)使用nohup后台执行脚本 脚本执行结果记录到nohup.out文件中 (二)使用&后台执行脚本 使用&符号在后台执行命令或脚本后,如果你退出登录,这个命令就会被自动终止掉

  5. Linux Shell脚本读写XML文件

    在Linux下如何用Shell脚本读写XML?现有一个config.xml <?xml version="1.0" encoding="UTF-8"?&g ...

  6. [转]Linux下的lds链接脚本详解

    转载自:http://linux.chinaunix.net/techdoc/beginner/2009/08/12/1129972.shtml     一. 概论 每一个链接过程都由链接脚本(lin ...

  7. 《Linux命令行与shell脚本编程大全》 第二十三章 学习笔记

    第二十三章:使用数据库 MySQL数据库 MySQL客户端界面 mysql命令行参数 参数 描述 -A 禁用自动重新生成哈希表 -b 禁用 出错后的beep声 -B 不使用历史文件 -C 压缩客户端和 ...

  8. Linux下的lds链接脚本简介

    转载:http://hubingforever.blog.163.com/blog/static/171040579201192472552886/   一. 概论 每一个链接过程都由链接脚本(lin ...

  9. linux的基本操作(shell 脚本的基础知识)

    shell 脚本的基础知识 日常的linux系统管理工作中必不可少的就是shell脚本,如果不会写shell脚本,那么你就不算一个合格的管理员.目前很多单位在招聘linux系统管理员时,shell脚本 ...

随机推荐

  1. 关于jQuery中.attr()和.prop()

    功能需求是这样的,两个radio:男和女,一个button:重置.启动页面默认选中男,在用户选择女之后又点击重置按钮,需要恢复到默认状态. <input type="radio&quo ...

  2. orapwd创建密码文件

    在CMD里输入命令如下:C:\Documents and Settings\Administrator>orapwd Usage: orapwd file=<fname> passw ...

  3. js 获取asp:dropdownlist选中的值

    var eSection = document.getElementById("<%=tx_ddlType.ClientID%>"); var eSectionValu ...

  4. CSS文字超出div或者span时显示省略号

    我们常常需要在文本过长时显示,将超出显示成省略号: 思想为: 首先设置宽度,然后让超出的部分隐藏如果有超出则在最后显示省略号让文本不换行 具体css代码为: .title{ width:200px;o ...

  5. asp.net 导出excel文件

    之前做过winfrom程序的导出excel文件的功能,感觉非常简单.现在试着做asp.net中导出excel的功能,之前用的是Microsoft.Office.Interop.Excel这个对象来实现 ...

  6. linux常用命令详解 (二)文件处理命令

    ◆ 文件处理命令:file.mkdir.grep.dd.find.mv.ls.diff.cat.ln: 系统信息存放在文件里,文件与普通的公务文件类似.每个文件都有自己的名字.内容.存放地址及其它一些 ...

  7. iOS 类管理

    CocoaPods安装和使用教程 Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaP ...

  8. poj2752Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  9. mysql学习(九)sql语句

    SQL种类: DDL:数据定义语言 DML:数据操作语言 DQL:数据查询语言 DCL:数据控制语言 DDL: show databases; //查询数据库 create database if n ...

  10. svn 查看某个时间段的记录

    调出svn后,点击查找历史的按钮.