Register
It is currently Thu Sep 18, 2014 11:44 am

Help with script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Jun 28, 2012 1:24 am   

Joined: Thu Jun 28, 2012 1:11 am
Posts: 1
Hi, please can some one help me. I have problem with one nested loop in bash script.
Code:
#! /bin/bash
echo -e '<?xml version="1.0" encoding="UTF-8"?>'  >> Extra_testplan.xml

SUBSECTIONCOUNTER=0
SUBSCASECOUNTER=0
HEADSUBSECTIONCOUNTER=0

echo -e "<suite>\n\t<id></id>\n\t<name></name>\n\t<description></description>\n\t<sections>\n" >> Extra_testplan.xml


while [ $HEADSUBSECTIONCOUNTER -lt 1 ]; do
let HEADSUBSECTIONCOUNTER=HEADSUBSECTIONCOUNTER+1

echo -e "\t\t<section>\n\t\t\t<name></name>\n\t\t\t<sections>" >> Extra_testplan.xml

while [ $SUBSECTIONCOUNTER -lt 2 ]; do
let SUBSECTIONCOUNTER=SUBSECTIONCOUNTER+1

echo -e "\n\n\t\t\t<section>\n\t\t\t<name></name>\n\t\t\t<cases>" >> Extra_testplan.xml


while [ $SUBSCASECOUNTER -lt 3 ]; do
let SUBSCASECOUNTER=SUBSCASECOUNTER+1

echo -e "\t\t\t\t<case>\n\t\t\t\t<id></id>\n\t\t\t\t<title></title>\n\t\t\t\t\t\t\t\t\t\t\t\t<type>Functional</type>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<priority>3 - Must Test</priority\n\t\t\t\t<estimate></estimate>\n\t\t\t\t<milestone></milestone>\n\t\t\t\t<references></references>\n\t\t\t</case>\n" >> Extra_testplan.xml


done
echo -e "\t\t\t</cases>\n\t\t\t</section>\n\n\n" >> Extra_testplan.xml

done
echo  -e "\t\t</sections>\n\t\t</section>" >> Extra_testplan.xml
done

echo -e "\t</sections>\n</suite>" >> Extra_testplan.xml

wait
date
exit 0


In first iteration everything it is ok, but in second iteration the third "while" doesn't work:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<suite>
   <id></id>
   <name></name>
   <description></description>
   <sections>

      <section>
         <name></name>
         <sections>


         <section>
         <name></name>
         <cases>
            <case>
            <id></id>
            <title></title>
                                    <type>Functional</type>
                                             <priority>3 - Must Test</priority
            <estimate></estimate>
            <milestone></milestone>
            <references></references>
         </case>

            <case>
            <id></id>
            <title></title>
                                    <type>Functional</type>
                                             <priority>3 - Must Test</priority
            <estimate></estimate>
            <milestone></milestone>
            <references></references>
         </case>

            <case>
            <id></id>
            <title></title>
                                    <type>Functional</type>
                                             <priority>3 - Must Test</priority
            <estimate></estimate>
            <milestone></milestone>
            <references></references>
         </case>

         </cases>
         </section>





         <section>
         <name></name>
         <cases>
                "!!!!!!!!! Must contain <case> sections!!!!"
         </cases>
         </section>



      </sections>
      </section>
      <section>
         <name></name>
         <sections>
      </sections>
      </section>
   </sections>
</suite>


Top
 Profile  
 PostPosted: Thu Jun 28, 2012 8:16 am   

Joined: Tue Jun 26, 2012 2:55 pm
Posts: 7
I'm not sure what you are trying to say man but could you maybe use a For loop instead of a while?


Top
 Profile WWW  
 PostPosted: Thu Jun 28, 2012 1:36 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
#!/bin/bash

xml_file="Extra_testplan.xml"

wanted_sections=1
wanted_sub_sections=2
wanted_cases=3


########


exec 3> "$xml_file"


cat >&3 << _XML_HEADER_
<?xml version="1.0" encoding="UTF-8"?>
<suite>
   <id></id>
   <name></name>
   <description></description>
   <sections>
_XML_HEADER_

for ((sections = 0; sections < wanted_sections; sections++)); do
   cat >&3 << _SEC_HEADER_
   
      <section>
         <name></name>
         <sections>
_SEC_HEADER_

   for ((sub_sections = 0; sub_sections < wanted_sub_sections;
        sub_sections++)); do
      cat >&3 << _SUB_SEC_HEADER_


         <section>
         <name></name>
         <cases>

_SUB_SEC_HEADER_

      for ((cases = 0; cases < wanted_cases; cases++)); do
         cat >&3 << _CASES_
            <case>
               <id></id>
               <title></title>
               <type>Functional</type>
               <priority>3 - Must Test</priority>
               <estimate></estimate>
               <milestone></milestone>
               <references></references>
            </case>

_CASES_
      done
      cat >&3 << _SUB_SEC_FOOTER_
         </cases>
         </section>
_SUB_SEC_FOOTER_
   done
   cat >&3 << _SEC_FOOTER_


         </sections>
      </section>
_SEC_FOOTER_
done

cat >&3 << _XML_FOOTER_

   </sections>
</suite>
_XML_FOOTER_


exec 3>&-


Top
 Profile  
 PostPosted: Thu Jun 28, 2012 2:00 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
hi,

counter variables need to be reset
Code:
#!/bin/bash

echo -e '<?xml version="1.0" encoding="UTF-8"?>'  >> Extra_testplan.xml
echo -e "<suite>\n\t<id></id>\n\t<name></name>\n\t<description></description>\n\t<sections>\n" >> Extra_testplan.xml
echo -e "\t\t<section>\n\t\t\t<name></name>\n\t\t\t<sections>" >> Extra_testplan.xml

while ((ssc++ < 2))
do
   echo -e "\n\n\t\t\t<section>\n\t\t\t<name></name>\n\t\t\t<cases>" >> Extra_testplan.xml

   while ((sscc++ < 3))
   do
      echo -e "\t\t\t\t<case>\n\t\t\t\t\t<id></id>\n\t\t\t\t\t<title></title>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<type>Functional</type>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<priority>3 - Must Test</priority\n\t\t\t\t<estimate></estimate>\n\t\t\t\t<milestone></milestone>\n\t\t\t\t<references></references>\n\t\t\t</case>\n" >> Extra_testplan.xml
   done
   unset sscc

   echo -e "\t\t\t</cases>\n\t\t\t</section>\n\n\n" >> Extra_testplan.xml
done

echo  -e "\t\t</sections>\n\t\t</section>" >> Extra_testplan.xml
echo -e "\t</sections>\n</suite>" >> Extra_testplan.xml

you could also put text of different header, sections and footer into variables; for example:
Code:
header="foo
   bar
      baz"
and so on
rather than write a hard-to-read very long line


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP