I have written the script, which unzips a zip file (passed as argument to the bash), loops through directory and files, then perform different actions based on directory or file. It is working fine.
However I would like to know some things:
- Is it possible to do this without the unzip command? I somehow feel like that step can be avoided and we can directly use the zip file to perform the task.
- I have used three temporary files/directories(tmp.txt - to store the directory and file lines, out.xml - for the curl command, DIREC - for finding the files and directories). I have removed those three files at the end of the script. Is there a way to optimize this? may be using variables instead of files.
Here is the bash script, with comments:
#!/bin/bash
#The below line unzips the zip file as passed as argument
unzip $1
length=${#1}
const=4
count=$((length-const))
DIREC=$(echo $1 | cut -b 1-$count)
OUTPUTFIL=out.xml
#The below line will find the directory, which was created by the unzip command. Creation of tmp.txt here.
find $DIREC -exec echo {} \; >tmp.txt
exec<tmp.txt
while read line
do
if [ -d "${line}" ] ; then
echo "$line is a directory";
else
if [ -f "${line}" ]; then
BASE=$(base64 $line);
echo "${line} is a file";
{
echo "<inpu>${BASE}</inpu>"
} > ${OUTPUTFIL}
curl -u admin:secret --data-binary @out.xml http://dp1-l2.com; echo
else
echo "${line} is not valid";
exit 1
fi
fi
done
rm -f tmp.txt
rm -rf $DIREC
rm -f out.xml