Operating System - Tru64 Unix
1754229 Members
3448 Online
108812 Solutions
New Discussion юеВ

Re: need help with shell script for array and loop processing files

 
Ben_181
Occasional Contributor

need help with shell script for array and loop processing files

I have a ton of database files that I want to parse and send to report text files. Currently we are using diffs to extract the information but would like to move this to a simpler method. I need create an array of all the files to loop thru and take a diff of the files
write the output to a .txt flat file in unix.

what I have thus far: (old script)

diff ${REPORT_DIR}_db_dy_${ORACLE_SID}_a.dat ${REPORT_DIR}_db_dy_${ORACLE_SID}_b.dat > diffs.txt

I want to take these about 100 of these files and make an array and process all 100 of them in a ksh loop to separate reports in flat files. Can anyone help me with how to do this in a ksh script? Thanks!!
4 REPLIES 4
Michael Schulte zur Sur
Honored Contributor

Re: need help with shell script for array and loop processing files

Hi,

no need to use arrays for this.
#!/bin/ksh
rm diffs.txt
SIDLIST="SID1 SID2 ... SID100"
for SID in ${SIDLIST}
do
diff ${REPORT_DIR}_db_dy_${ORACLE_SID}_a.dat ${REPORT_DIR}_db_dy_${ORACLE_SID}_b.dat >> diffs.txt
done

greetings,

Michael
Michael Schulte zur Sur
Honored Contributor

Re: need help with shell script for array and loop processing files

Hi,

hit the button too early. That should have been
diff ${REPORT_DIR}_db_dy_${SID}_a.dat ${REPORT_DIR}_db_dy_${SID}_b.dat > diffs.txt

Michael
Ben_181
Occasional Contributor

Re: need help with shell script for array and loop processing files

Thanks. Actually my goal is to streamline the whole process so that if we add another script to run a report against I can just add it to the array stack without adding another messy diff command.

example:

diff ${REPORT_DIR}r${YESTERDAY}_db_dy_${ORACLE_SID}_report1.dat
${REPORT_DIR}r${TODAY}_db_dy_${ORACLE_SID}_report2.dat
>${REPORT_DIR}r${TODAY}_db_dy_${ORACLE_SID}_diffs.dat

-- echo reports to files
echo "${ORACLE_SID} RESULTS FOR ${TODAY}">${REPORT_DIR}r${TODAY}_db_dy_${ORACLE_SID}_diffrepots.txt

As you can see when you have 100 of these diff files and need to echo them to a flat file the shell script gets very messy and confusing! I want to clean the logic up by using an array in a loop so that when a new batch of files are added I just add a new array member to the stack. Thank you very much!
Michael Schulte zur Sur
Honored Contributor

Re: need help with shell script for array and loop processing files

Ben,

in case you did not notice, I was ruuning a for loop. I have only one diff.
But if you want an array, that is no problem.
You use arrays like
SIDLIST[0]="sid1"
They have one index, are created automatically and can hold up to 1023 members.

greetings,

Michael