在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
2016-10-31 16:44
651 查看
在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来写入。
#!/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。具体用法见这个网页。
<?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脚本
- Linux中如何防止一个Shell脚本被重复执行?
- linux下如何自动检测并重新启动一个死掉的进程(shell脚本实现)
- linux下如何自动检测并重新启动一个死掉的进程,然后再把它杀死:)(shell脚本实现)
- linux0.11是如何加载并执行一个二进制可执行文件和shell脚本文件的?
- Linux下两个远程登陆用户如何共享同一个登陆shell
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本。
- 如何在Linux系统编写 shell 脚本(初级)
- 如何编写一个shell脚本[转]
- LINUX下如何写SHELL脚本
- 如何编写一个shell脚本
- LINUX下如何写SHELL脚本
- 在LINUX平台下如何写SHELL脚本
- windows下编写的shell脚本如何在Linux运行
- 如何编写一个shell脚本(转贴)
- 如何用Shell病毒技术感染Linux脚本程序
- 如何编写一个shell脚本
- 如何编写一个shell脚本
- 如何编写一个shell脚本
- 如何编写一个shell脚本