如何使我从 Linux 脚本中获取的那些值成为我的 XML 文件中的值

如何使我从 Linux 脚本中获取的那些值成为我的 XML 文件中的值

我有一个问题,如何让我从 bash 脚本中获取的值成为我的 XML 文件的值。下面是我的 XML 文件:

<?xml version="1.0"?>
<List>
  <Info>
    <id></id>
    <date></date>
    <time></time>
    <num></num>
    <type></type>
  </Info>
  <Info>
    <id></id>
    <date></date>
    <time></time>
    <num></num>
    <type></type>
  </Info>
  <Info>
    <id></id>
    <date></date>
    <time></time>
    <num></num>
    <type></type>
 </Info>
 <Info>
    <id></id>
    <date></date>
    <time></time>
    <num></num>
    <type></type>
  </Info>
  <Info>
    <id></id>
    <date></date>
    <time></time>
    <num></num>
    <type></type>
  </Info>
</List>

这是我的 bash 脚本:

#!/bin/bash

if [ "$(egrep -l 'NU' /home/archive/* |  xargs egrep -l 'SE')" ]
then
  egrep -l 'NU' /home/archive/* | xargs egrep -l 'SE' > /tmp/result.txt
  chmod 777 /tmp/result.txt

  a=1
  while read LINE
  do
    path[$a]=$LINE
    file[$a]=`basename $LINE`
    cat $LINE | awk '{gsub("^",RS);print}' > /tmp/tmp2.result.txt
    chmod 777 /tmp2/tmp.result.txt
    cat /tmp/tmp.result.txt | awk '{gsub("-",RS);print}' > /tmp/tmp.result.txt
    chmod 777 /tmp/tmp.result.txt
    ID[$a]=`sed -n '7p' < /tmp/tmp.result.txt`
    DATE[$a]=`sed -n '10p' < /tmp/tmp.result.txt`
    TIME[$a]=`sed -n '11p' < /tmp/tmp.result.txt`
    NUM[$a]=`sed -n '14p' < /tmp/tmp.result.txt`
    TYPE[$a]=`sed -n '26p' < /tmp/tmp.result.txt`

    # echo -e "File: ${file[$a]}" >> /tmp/result.result.tmp  
    # echo -e "Type:${TYPE[$a]}\ID: ${ID[$a]}\tDATE:${DATE[$a]}\tTIME:${TIME[a]}\tNUM: ${NUM[$a]}" >> /tmp/result.result.tmp

  done < /tmp/result.txt
fi

答案1

你可以编写一个printf模板:

info_template="  <Info>\n\
    <id>%s</id>\n\
    <date>%s</date>\n\
    <time>%s</time>\n\
    <num>%s</num>\n\
    <type>%s</type>\n  </Info>\n"

然后构造 XML 文件:

#!/bin/bash


if [ "$(egrep -l 'NU' /home/archive/* |  xargs egrep -l 'SE')" ]
then
  egrep -l 'NU' /home/archive/* | xargs egrep -l 'SE' > /tmp/result.txt
  chmod 777 /tmp/result.txt

  # the output XML file
  my_xml_file=~/data.xml

  # the spaces are for pretty-ing up the output
  info_template="  <Info>\n\
    <id>%s</id>\n\
    <date>%s</date>\n\
    <time>%s</time>\n\
    <num>%s</num>\n\
    <type>%s</type>\n  </Info>\n"

  # add the opening tags
  echo '<?xml version="1.0"?>' >> $my_xml_file
  echo '<List>' >> $my_xml_file

  a=1
  while read LINE
  do
    path[$a]=$LINE
    file[$a]=`basename $LINE`
    cat $LINE | awk '{gsub("^",RS);print}' > /tmp/tmp2.result.txt
    chmod 777 /tmp2/tmp.result.txt
    cat /tmp/tmp.result.txt | awk '{gsub("-",RS);print}' > /tmp/tmp.result.txt
    chmod 777 /tmp/tmp.result.txt
    ID[$a]=`sed -n '7p' < /tmp/tmp.result.txt`
    DATE[$a]=`sed -n '10p' < /tmp/tmp.result.txt`
    TIME[$a]=`sed -n '11p' < /tmp/tmp.result.txt`
    NUM[$a]=`sed -n '14p' < /tmp/tmp.result.txt`
    TYPE[$a]=`sed -n '26p' < /tmp/tmp.result.txt`

    # echo -e "File: ${file[$a]}" >> /tmp/result.result.tmp  
    # echo -e "Type:${TYPE[$a]}\ID: ${ID[$a]}\tDATE:${DATE[$a]}\tTIME:${TIME[a]}\tNUM: ${NUM[$a]}" >> /tmp/result.result.tmp

    printf "$info_template" "$ID[$a]"   \
                            "$DATE[$a]" \
                            "$TIME[$a]" \
                            "$NUM[$a]"  \
                            "$TYPE[$a]" >> $my_xml_file
  done < /tmp/result.txt

  # don't forget the closing tag
  echo '</List>' >> $my_xml_file
fi

相关内容