Como fazer esses valores que recebo do meu script do Linux para se tornarem os valores no meu arquivo XML [closed]

0

Eu tenho um problema sobre como posso fazer os valores que recebo do meu script bash para tornar-se o valor para o meu arquivo XML. Abaixo está meu arquivo 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>

Aqui está meu script 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
    
por Anne 16.07.2014 / 03:31

1 resposta

1

Você pode escrever um modelo 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"

Em seguida, construa o arquivo 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
    
por edwin 16.07.2014 / 04:43