Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate XML File with Shell script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use Shell script to generate XML files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The details are as follows:

The requirement is to write a shell script, the installation requires querying the data, assembling the qualified data according to the xml style given by the customer, then adding it to the crontab, and regularly executing it to the client server through scp or ftp.

Specific implementation steps:

First, write code to generate xml documents

#! / bin/bash# filename: the first parameter passed in by create_xml.sh# create_wangxb_20150123## from outside assembles a node as indent tabs=0# + + # in the file name outfile=$1# xml of xml, and outputs it to file # to explain the differences when passing parameters: if the command # / path/to/scriptname opt1 opt2 opt3 opt4 # $0: executed by the following script defaults to the name of the script Starting with $1 color 4 is the value of the parameter # $#: represents the following parameter "number" # $@: represents "$1", "$2", "$3" and "$4", each variable is independent (enclosed in double quotation marks) # $*: stands for "$1c$2c$3c$4", where c is a delimited byte and defaults to a blank key, so in this case it stands for "$1 $2 $3 $4". # in shell, we can also use ${} to include the variable name to call the variable # + + put () {echo'> > $outfile} # here is also the node that outputs a xml, but has more settings # ${@: 2} than the above node: its value starts from the second parameter to the last parameter, why? Sometimes you may have spaces in your second parameter. Shell accepts that the parameter put_tag () {echo''${@: 2}'> > $outfile} # calculated in spaces is also an output node function, but added CDATA Prevent xml parsing failure caused by special characters put_tag_cdata () {echo'> > $outfile} put_head () {put'?'${1}'?'} # this is an indentation algorithm Understand out_tabs () {tmp=0 tabsstr= "" while [$tmp-lt $((tabs))] do tabsstr=$ {tabsstr}'\ t 'tmp=$ ((tmp+1)) done echo-e-n $tabsstr > > $outfile} tag_start () {out_tabs put $1 tabs=$ ((tabs+1))} tag () {out_tabs if ["$1" = = 0] then put_tag $2 $(echo ${@: 3}) elif ["$1" = = 1] then put_tag_cdata $2 $(echo ${@: 3}) fi} tag_end () {tabs=$ ((tabs-1)) out_tabs put'/'${1}}

Here are some basics:

About parameters:

If there is a command executed by the following script

/ path/to/scriptname opt1 opt2 opt3 opt4

The value of $0: is the name of the script by default, starting with $1Must4 is the value of the parameter $#: represents the "number" of the following parameters $@: represents "$1", "$2", "$3" and "$4", each variable is independent (enclosed in double quotes) $*: stands for "$1c$2c$3c$4", where c is a delimited byte and defaults to a blank key, so in this case it stands for "$1 $2 $3 $4". In shell, we can or can use ${} to include the variable name to call the variable

Second, look up the data from the database and use the above functions to make xml files.

#! / bin/bash# filename: ts_xml.sh# create_wangxb_20150126#PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:/home/p3s_batch/tools:/home/p3s_batch/binexport PATH# Database account information filesource ~ / .p3src # + + # set some variable # XMLSCRIPT: absolute path to the script # MATCHING _ RESULT_XML: file name of xml_1 # XML_FUNC_FILE: generate xml function file path # MATCHING_RESULT_QUERY_DATA: 00:00 file checked by sqlplus data saved # MATCHING_RESULT_QUERY_SQL: sql statement of sqlplus query # + + # here are some basic settings export XMLSCRIPT=/usr/p3s/batch/jaaa_match/tmp_xa_wangxbXML_DIR= "$XMLSCRIPT/xmldata" XML_FUNC_FILE= "xml_func.sh" MATCHING_ RESULT_XML= "matching_result_" $(date'+% Y% m% dwells% H% M% S') ".xml" MATCHING_RESULT_QUERY_DATA= "matching_result_query_data.tmp" MATCHING_RESULT_QUERY_SQL= "matching_result_query.sql" CLIENT_LIST_XML= "client_list_" $(date'+% Y% m% dudes% H% M% S') ".xml" CLIENT_LIST_QUERY_DATA= "client_list_query_data.tmp" CLIENT_LIST_QUERY_SQL= "client_list_query.sql" # add_wangxb_20150225if [!-d "$XML_DIR"] Then mkdir $XML_DIRfi#++# modify_wangxb_20150224# check for temporary file # + if [- e "$XML_DIR/$MATCHING_RESULT_XML"]; then rm-f $XML_DIR/$MATCHING_RESULT_XMLfiif [- e "$XMLSCRIPT/$MATCHING_RESULT_QUERY_DATA"] Then MATCHING_RESULT_QUERY_DATA= "matching_result_query_data_" $(date'+% Y% m% d% H% M% S') ".tmp" fi#+++# add_wangxb_20150225# check system time, choice query time period# is based on the daily execution time of crontab Get the time interval of the where condition when we query the database # + + sys_datetime=$ (date'+% Y% m% d% H') first_chk_datetime= "$(date'+% Y% m% d') 04" second_chk_datetime= "$(date'+% Y% m% d') 12" third_chk_datetime= "$(date'+% Y% m% d') 20" # because the server crontab is the above time, but more shell is executed When I call my shell, it doesn't have to be 04:30, 12:30, 20:30, so Here, according to the time range of the system, case $sys_datetime in "$first_chk_datetime" | "$(date'+% Y% m% d') 05" | "$(date'+% Y% m% d') 06" | "$(date'+% Y% m% d') 07") chk_start=$ (date'+% Y-%m-%d 2100rig 00'-d'-1 day') ) chk_end=$ (date'+% Y-%m-%d 04VG 29JV 59') ; "$second_chk_datetime" | "$(date'+% Y% m% d') 13" | "$(date'+% Y% m% d') 14" | "$(date'+% Y% m% d') 15") chk_start=$ (date'+% Y-%m-%d 0430 date') chk_end=$ (date'+% Y-%m-%d 1229 date 59') "$third_chk_datetime" | "$(date'+% Y% m% d') 21" | "$(date'+% Y% m% d') 22" | "$(date'+% Y% m% d') 23") chk_start=$ (date'+% Y-%m-%d 123000date') chk_end=$ (date'+% Y-%m-%d 2059 date') *) chk_start=$ (date'+% Y-%m-%d 0000lv 0000') chk_end=$ (date'+% Y-%m-%d 23lv 59') Esac# modify_wangxb_20150310# below is to do a test of an oracle database connection. If the connection fails, the subsequent code will not be executed. And write the error log $ORACLE_HOME/bin/sqlplus-s $ORAUSER_WEB_PASDB and output it to the specified file $ORACLE_HOME/bin/sqlplus-s $ORAUSER_WEB_PASDB @ $XMLSCRIPT/$MATCHING_RESULT_QUERY_SQL "$chk_start"$chk_end" > $XMLSCRIPT/$MATCHING_RESULT_QUERY_DATA# create matching result's xml file# add_wangxb_20150227#. The algorithm below is to analyze the checked data. Call the xml function to generate the xml file source "$XMLSCRIPT/$XML_FUNC_FILE"$XML_DIR/$MATCHING_RESULT_XML" put_head 'xml version= "1.0" encoding= "utf-8"' tag_start 'ROOT'if [- s "$XMLSCRIPT/$MATCHING_RESULT_QUERY_DATA"] Then datas=$ {XMLSCRIPT} / ${MATCHING_RESULT_QUERY_DATA} # for res in $datas while read res Do stock_id=$ (echo $res | awk 'BEGIN {FS= "\ ^\ *\ ^') seirino=$ (echo $res | awk 'BEGIN {FS="\ ^\ *\ ^ "} {print $2}') match_flg=$ (echo $res | awk 'BEGIN {FS="\ ^\ *\ ^ "} {print $3}') unmatch_riyuu=$ (echo $res | awk 'BEGIN { FS= "\ ^\ *\ ^"} {print $4}') up_date_tmp=$ (echo $res | awk 'BEGIN {FS= "\ ^\ *\ ^"} {print $5}') up_date=$ (echo $up_date_tmp | awk 'BEGIN {FS= "@"} {print $1 "" $2}') tag_start 'MATCHING' tag 0' STOCKID' ${stock_id:- "" } tag 0 'SEIRINO' ${seirino:- "} tag 0' RESULT' ${match_flg:-"} tag 1 'REASON' ${unmatch_riyuu:-"} tag 0' UPDATE_DATE' ${up_date:-"} tag_end 'MATCHING' done

< $datasfitag_end 'ROOT'rm $XMLSCRIPT/$MATCHING_RESULT_QUERY_DATA# create client list's xml file# add_wangxb_2015027# 下面的是再生成一个xml文件,和上面一样if [ -e "$XML_DIR/$CLIENT_LIST_XML" ];then rm -f $XML_DIR/$CLIENT_LIST_XMLfiif [ -e "$XMLSCRIPT/$CLIENT_LIST_QUERY_DATA" ];then CLIENT_LIST_QUERY_DATA="client_list_query_data_"$(date '+%Y%m%d%H%M%S')".tmp"fi$ORACLE_HOME/bin/sqlplus -s $ORAUSER_MND @$XMLSCRIPT/$CLIENT_LIST_QUERY_SQL >

$XMLSCRIPT/$CLIENT_LIST_QUERY_DATAsource "$XMLSCRIPT/$XML_FUNC_FILE"$XML_DIR/$CLIENT_LIST_XML" put_head 'xml version= "1.0" encoding= "utf-8"' tag_start 'ROOT'if [- s "$XMLSCRIPT/$CLIENT_LIST_QUERY_DATA"]; then datas=$ {XMLSCRIPT} / ${CLIENT_LIST_QUERY_DATA} # for res in $datas while read res Do corporation_id=$ (echo $res | awk 'BEGIN {FS= "\ ^\ *\ ^') corporation_name=$ (echo $res | awk 'BEGIN {FS="\ ^\ *\ ^ "} {print $2}') client_id=$ (echo $res | awk 'BEGIN {FS="\ ^\ *\ ^ "} {print $3}') client_print_name=$ (echo $res | Awk 'BEGIN {FS= "\ ^\ *\ ^"} {print $4}') tag_start 'CLIENT' tag 0' CORPORATION_ID' ${corporation_id:- ""} tag 1 'CORPORATION_NAME' ${corporation_name:- ""} tag 0' CLIENT_ID' ${client_id:- ""} tag 1 'CLIENT_PRINT_NAME' ${client_print_name:- " "} tag_end 'CLIENT' done < $datasfitag_end' ROOT'rm $XMLSCRIPT/$CLIENT_LIST_QUERY_DATA# add_wangxb_20150304# Convert xml file encoding# this transcodes the xml file The command is iconvif [- e "$XML_DIR/$MATCHING_RESULT_XML"] Then echo "* matching_result.xml euc-jp-t utf-8 $XML_DIR/$MATCHING_RESULT_XML-o $XML_DIR/$MATCHING_RESULT_XML.utf-8 mv $XML_DIR/$MATCHING_RESULT_XML.utf-8 $XML_DIR/$MATCHING_RESULT_XMLfiif [- e" $XML_DIR/$CLIENT_LIST_XML "] Then echo "* client_list.xml sagging, * "iconv-f euc-jp-t utf-8 $XML_DIR/$CLIENT_LIST_XML-o $XML_DIR/$CLIENT_LIST_XML.utf-8 mv $XML_DIR/$CLIENT_LIST_XML.utf-8 $XML_DIR/$CLIENT_LIST_XMLfi# add_wangxb_20150304# Send the xml file to the destination server by ftp#ftp_host=" 222.collect * "# USER=" * * "# PASS=" * * "# ftp-I-n $ftp_host"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report