在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脚本 ...
随机推荐
- 类加载器与methodinterceptor接口
类加载器: JVM将类加载过程分为三个步骤: 装载(Load):加载二进制文件 链接(Link)进行了验证:验证文件准确性 准备:将静态变量进行分配内存,初始化其默认值. 解析:符号引用转换为直接引用 ...
- leetcode_question_125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 依赖注入及AOP简述(十)——Web开发中常用Scope简介 .
1.2. Web开发中常用Scope简介 这里主要介绍基于Servlet的Web开发中常用的Scope. l 第一个比较常用的就是Application级Scope,通常我们会将一 ...
- Android 动画之ScaleAnimation应用具体解释
android中提供了4中动画: AlphaAnimation 透明度动画效果 ScaleAnimation 缩放动画效果 TranslateAnimation 位移动画效果 RotateAnimat ...
- vbox下安装 linux 64 bit出现“kernel requires an x86_64 cpu
今天在vbox下安装linux 64bit出现"kernel requires an x86_64 cpu, but only detected "的错误,网上有很多文章介 ...
- SQL查询练习题目
SQL查询练习题目 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示 ...
- SQL Server 死锁检查
示例代码 select spid, blocked, status, hostname, program_name, hostprocess, cmd from sysprocesses -- kil ...
- JavaScript Dispatch Event
<html> <head> <script type="text/javascript"> function performClick(elem ...
- Linux下安装McAfee防病毒软件(企业版本)
最近公司接一个项目虚拟化解决方案,不过所有硬件设备不是我们采购的,我们只是负责软体安装.我看了一下那个硬件设备那叫高,不过目前还到那边去安装,那边硬件还没安装完成,然后Boss给我拿来两台新服务器,让 ...
- juce中的BailOutChecker
界面库中值得注意的一点就是对象响应事件的时候自身被删除了,那么后续的访问自然就会出问题,所以需要在响应事件之后先添加引用,相关处理之后再查看自身是否已经被删除,如果已经被删除那么就直接退出.juce中 ...