在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. Msys+Mingw在手 妙用在心!

    1 缘起 平时在一些c++群面,看见很多大学十分努力的学习c++/MFC ,看见在编程语言百花争芳的时候,C/C++还是很有很有魅力.估计很多初学者使用都是window下的visual stdio 开 ...

  2. ios里的UIActionSheet的使用

    class ViewController: UIViewController,UIActionSheetDelegate{ @IBOutlet weak var label1: UILabel! @I ...

  3. 网络笔记01-2 scoket

    scoket: 1.socket /** 第一个参数(domain): 表示用什么协议 AF_INET 为IPV4开发 第二个参数(type): 表示scoket为什么类型SOCK_STREAM为TC ...

  4. perl 脚本测试

      原文地址:  http://blog.csdn.net/johnny710vip/article/details/8905239   这是一篇关于perl脚本测试的总结性文章,其中提到了很多实用的 ...

  5. Ubuntu 常用软件安装方法

    macubuntu 安裝方法: $wget https://github.com/downloads/ChinaLuo/Mac_Ubuntu/Mac_Ubuntu-12.04.tar.gz -O /t ...

  6. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  7. kali linux安装vm

    https://download3.vmware.com/software/wkst/file/VMware-Workstation-Full-10.0.2-1744117.i386.bundle v ...

  8. c++ 如何实现,readonly

    需求: 我要实现一个常量字段,只能赋值一次,赋值后不容许更改. 类似于c#的readonly或者java final #include <iostream> class  A{public ...

  9. 【BZOJ】【1834】【ZJOI2010】Network 网络扩容

    网络流/费用流 这题……我一开始sb了. 第一问简单的最大流…… 第二问是要建费用流的图的……但是是在第一问的最大流跑完以后的残量网络上建,而不是重建…… 我们令残量网络上原有的弧的费用全部为0(因为 ...

  10. 树形dp求树的重心

    Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...