bash Arrays to fill in xml file, array inside an array?

Viewed 27

I have an xml file I am trying to create with a bash script and it is not too difficult with the echo command. However I need to grab all of the files in a directory which are a cad type and bring it in to the xml. I do this with an array getting the entire directory path as it is needed for the first part of the xml. This is where it gets difficult because I need to strip off the path and extension and place this into the xml 3 or 4 times for each item in the folder. I have tried an array inside an array but this just makes more lines then needed.

Here is a sample of what it needs to look like. this is for 1 file in the folder and there could be 20 or more and I will not know at anytime how many. My script has changed many times but here is a version that is close. see below sample.

<?xml version="1.0" encoding="utf-8"?>
<Program version="1.2">
    <Input>
        <Name>Demo</Name>
    </Input>
    <Parts>
        <Part>
            <Input>
                <File>C:\opti\O0904\cnhbensonol1\DXF FILES\20038100.SLDPRT</File>
                <Machine>AutoPOL</Machine>
                <OnlyUnfolder>true</OnlyUnfolder>
                <SaveSettings>
                    <NCFile>
                    <Save>true</Save>
                    <Directory>C:\opti\O0904\cnhbensonol1\OUTPUT\NC FILES</Directory>
                    <File>20038100</File>
                    </NCFile>
                    <Graphics>
                        <Save>true</Save>
                        <Directory>C:\opti\O0904\cnhbensonol1\OUTPUT\NC FILES</Directory>
                        <File>20038100</File>
                    </Graphics>
                    <FlatPatternDXF>
                        <Save>true</Save>
                        <Directory>C:\opti\O0904\cnhbensonol1\OUTPUT\DXF FILES</Directory>
                        <DXFSetting>OPTI</DXFSetting>
                    <File>20038100</File>
                    </FlatPatternDXF>
                    <AutoPOLFile>
                        <Save>true</Save>
                        <Directory>C:\opti\O0904\cnhbensonol1\OUTPUT\POLP FILES</Directory>
                        <File>20038100</File>
                    </AutoPOLFile>
                    <ProcessDocumentation>
                        <Save>true</Save>
                        <Directory>C:\opti\O0904\cnhbensonol1\OUTPUT\PDF FILES</Directory>
                        <File>20038100</File>
                    </ProcessDocumentation>
                </SaveSettings>
            </Input>
        </Part>
    </Parts>
</Program>

code below is my Bash script. I know there are better ways but I don't have access or know most of the others such as python etc.

#!/bin/bash
set -x
# this will grab files from the designated folder and create an xml
# Batch and Remote which runs in the background


ls "C:/opti/o0904/cnhbensonol1/parts2do/"*.* > tmp  #folder with cad files
newpath=$(cygpath -w "C:/opti/O0904/cnhbensonol1/parts2do/"*.*) # needs paths with \
rm tmp      #it works
echo "$newpath" > tmp

file_in="tmp"
file_out="input.apProgram"


# static text formatted

header='<?xml version="1.0" encoding="utf-8"?>
<Program version="1.2">
    <Input>
        <Name>OPTI</Name>
    </Input>
    <Parts>'

echo "$header" > $file_out
while IFS=$',' read -r -a arry; #this array fills in the cad file for the directory. 
do
  echo '        <Part>
            <Input>' >> $file_out
  echo '                <File>'${arry[0]}'</File>' >> $file_out
  echo '                <OnlyUnfolder>true</OnlyUnfolder>' >> $file_out
  echo '                <SaveSettings>
                    <NCFile>
                    <Save>true</Save>
                    <Directory>C:\opti\o0904\cnhbensonol1\OUTPUT\NC FILES</Directory>' >> $file_out
cat tmp | rev | cut -d"\\" -f1 | rev | cut -f1 -d "." > out.txt 

    line=`cat C:/opti/O0904/cnhbensonol1/out.txt`
    while IFS= read -r line
    do
echo '                  <File>'$line'</File>
                    </NCFile>
                    <Graphics>
                        <Save>true</Save>
                        <Directory>C:\opti\o0904\cnhbensonol1\OUTPUT\NC FILES</Directory>
                        <File>'$line'</File>
                    </Graphics>
                    <FlatPatternDXF>
                        <Save>true</Save>
                        <Directory>C:\opti\o0904\cnhbensonol1\OUTPUT\DXF FILES</Directory>
                        <DXFSetting>OPTI</DXFSetting>
                    <File>'$line'</File>
                    </FlatPatternDXF>
                    <AutoPOLFile>
                        <Save>true</Save>
                        <Directory>C:\opti\o0904\cnhbensonol1\OUTPUT\POLP FILES</Directory>
                        <File>'$line'</File>
                    </AutoPOLFile>
                    <ProcessDocumentation>
                        <Save>true</Save>
                        <Directory>C:\opti\o0904\cnhbensonol1\OUTPUT\PDF FILES</Directory>
                        <File>'$line'</File>
                    </ProcessDocumentation>
                </SaveSettings>
            </Input>' >> $file_out
    done < out.txt
echo '      </Part>' >> $file_out
done < $file_in

printf "%s\n" "header"
echo '    </Parts>
</Program>' >> $file_out
rm out.txt 
rm tmp

Picture of the folder that I am inputting.

enter image description here

0 Answers
Related