在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。具体用法见这个网页。

http://www.cnblogs.com/Mainz/archive/2012/09/22/2697955.html

Linux Shell脚本读写XML文件的更多相关文章

  1. 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)

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

  2. shell脚本生成xml文件

    今天把这段时间学习完shell后完成工作上的一个小案件整理了一下,分享给大家! 说来也巧了,作为一个刚刚毕业半年的菜鸟,进入公司后,听公司的大牛推荐学习linux--”鸟哥的私房菜“,基本上是从去年8 ...

  3. 【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理

    要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh  input_path  output_ ...

  4. linux下shell脚本执行jar文件

    最近在搞一个shell脚本启动jar文件个关闭jar文件的东东.搞得我都蛋疼了.今天晚上终于弄好了 话说,小弟的linux只是刚入门,经过各方查资料终于搞定了.话不多说,下面开始上小弟写的shell脚 ...

  5. Linux shell 脚本攻略之统计文件的行数、单词数和字符数

    摘自:<Linux shell 脚本攻略>

  6. Linux shell 脚本攻略之创建不可修改文件

    摘自:<Linux shell 脚本攻略>

  7. Linux shell 脚本攻略之生成任意大小的文件

    摘自:<Linux shell 脚本攻略>

  8. Linux shell 脚本攻略之文件查找与文件列表

    摘自:<Linux shell 脚本攻略>

  9. LINUX SHELL脚本攻略笔记[速查]

    Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...

随机推荐

  1. 【转载】Powershell获取世纪互联Office365所有用户最后一次登录时间

    #$Mails=get-mailbox -ResultSize 10 $Mails=get-mailbox -ResultSize Unlimited $Mails | Measure-Object ...

  2. SQL Server 读取CSV中的数据

    测试: Script: create table #Test ( Name ), Age int, T ) ) BULK INSERT #Test From 'I:\AAA.csv' with( fi ...

  3. Mininet VM设置笔记

    Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...

  4. matlab实现复合梯形法则

    复合梯形法则: function int_f = CompoundEchelon( f, a, b, m ) % input : f : function handler % a : the lowe ...

  5. HDU3887 DFS序+ 线段树

    查询树上某个节点的子节点的标号小于其标号的数目. 一个trick是建立线段树之后,从标号小的向标号大的来做更新. 1: #include <cstdio> 2: #include < ...

  6. LintCode-Hash Function

    In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...

  7. android中的selector背景选择器的用法

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...

  8. C#: Create a WebRequest with HTTPClient

    http://www.cnblogs.com/shanyou/archive/2012/03/21/2410739.html http://msdn.microsoft.com/zh-cn/libra ...

  9. 管道Pipe

    管道Pipe java.nio.channels包中含有一个名为Pipe(管道)的类.广义上讲,管道就是一个用来在两个实体之间单向传输数据的导管.管道的概念对于Unix(和类Unix)操作系统的用户来 ...

  10. dragsort拖动插件的使用

    <!DOCTYPE html><html><head> <title>DragSort Example</title> <meta c ...