I was tasked at work to improve a system where through a web interface a client, which eventually became us, uploads a bunch of images with descriptions (latter coming from csv files), the images are then automatically resized yada yada for the site.
Nowt wrong with the latter, but the upload and image description data entry is unbelievably tedious - the latter having to be filled out by hand and the uploads done through http.
The usual way to do this would be for me to create a more sensible interface. However, since it's us that end up doing it and me being inexperienced at *nix sysadmin type stuff and wanting to gain some experience, I thought it would be more interesting to write a shell script to do it accessed by SSH, on the basis we can upload the CSVs and image files easily enough. Have also used sed, perl, and a line of php to help me encode data as it comes from the CSV.
With the exception of removing my company name from the logo, here is the unaltered code:
How could this shell script be improved?
#! /bin/bash
# Yes, the below is ludicrous overkill.
# B
# U
# T
# I need to become good enough at shell scripting that I don't have to look up
# elementary syntax before I write anything that is not just a collection of
# commands.
# It was fun :)
function quitTheBestShellScriptInTheWorld {
tput setaf 0
tput sgr0
if [[ $tempFile != "" ]]; then
rm $tempFile
fi
exit
}
function randomColour {
tput setaf $[ RANDOM % 8]
if [[ $1 == "e" ]]; then
echo
fi
}
function everyLineADifferentColour {
while read eachLine
do
randomColour
echo $eachLine
done
}
trap quitTheBestShellScriptInTheWorld 0 1 2 3 15 #let's be tidy
clear
tput bold
randomColour
if [[ $1 == "-l" ]]; then
echo "Er, logging feature not actually programmed. Add it yourself? Will be reasonably straightforward [y/n]"
read -n 1 dolog
if [[ $dolog == "y" ]]; then
(nano coolscript.sh)
else
echo 'Wise choice, if you change your mind then run again and answer yes, or just edit this script (which is all that pressing y would have done)'
fi
exit 0
fi
echo '
.___
| | _____ _____ ____ ____
| |/ \\__ \ / ___\_/ __ \
| | Y Y \/ __ \_/ /_/ > ___/
|___|__|_| (____ /\___ / \___ >
\/ \//_____/ \/
__________ .__
\______ \ ____ _____|__|_______ ___________
| _// __ \ / ___/ \___ // __ \_ __ \
| | \ ___/ \___ \| |/ /\ ___/| | \/
|____|_ /\___ >____ >__/_____ \\___ >__|
\/ \/ \/ \/ \/
'
randomColour
echo Enter settings, or press enter for defaults:
randomColour e
echo Loading directory paths from Perl
success=failure
cd ../www/properties/lib/
uploadDataStore=$(perl -e 'use WIMSConfig; $z = WIMSConfig->new(); print $z->{uploadDataStore}')
if [[ $? == 0 ]] && [[ $uploadDataStore != "" ]]; then
currentDataStore=$(perl -e 'use WIMSConfig; $z = WIMSConfig->new(); print $z->{currentDataStore}')
if [[ $? == 0 ]] && [[ $currentDataStore != "" ]]; then
success=success
fi
fi
if [[ $success != "success" ]]; then
randomColour
echo "Failure: Problem setting directory paths from perl code"
exit 100
fi
tput setaf 4
randomColour e
echo fyi, I have uploadDataStore of $uploadDataStore and currentDataStore of $currentDataStore
echo
success=no
while [[ $success == "no" ]]; do
randomColour
echo 'Which directory from within uploadDataStore to get files from? (default = subfolder)'
randomColour
read theDirectory
if [[ $theDirectory == "" ]]; then
theDirectory=subfolder
fi
if [[ -e $uploadDataStore$theDirectory ]]; then
success=yes
else
randomColour
echo "Can't find a folder called $uploadDataStore$theDirectory"
echo
fi
done
declare -i NameCounter
declare -A fileNameToNumberMap
declare -A numberToFileNameMap
NameBuilder='Submit=Save+and+Process'
success=no
while [[ $success == "no" ]]; do
randomColour e
echo 'What is name of CSV file? (default = csvfile.csv)'
randomColour
read CSVFilename
if [[ $CSVFilename == "" ]]; then
CSVFilename=csvfile.csv
fi
CSVFilename=$uploadDataStore$theDirectory/$CSVFilename
if [[ -e $CSVFilename ]]; then
success=yes
else
randomColour
echo Counld not find file by name of $CSVFilename, try again
fi
done
tempfile=$(mktemp)
echo -n $NameBuilder > $tempfile
cat $CSVFilename | php -r 'while(($a = fgets(STDIN))!==false) echo str_replace("---___---","\n",str_replace("-_.-_.",",",urlencode(str_replace("\n","---___---",str_replace(",","-_.-_.",$a)))));' | awk -F "," '{prefix=sprintf("%d_",NR);printf("&%s_Name=%s&%sdataStore=%s&%sbranch=%s&%sdevelopmentName=%s&%saltDevelopmentName=%s&%slocation=%s&%scompletionYear=%s&%sdevelopmentType=%s&%sdevelopmentComprises=%s&%sstageOfCompletion=%s&%sshotType=%s&%sdetail%s&%simageType=%s&%scopyright=%s&%sphotoExists=%s&%soriginalFilename=%s",prefix,$1,prefix,$3,prefix,$4,prefix,$5,prefix,$6,prefix,$7,prefix,$8,prefix,$9,prefix,$10,prefix,$11,prefix,$12,prefix,$13,prefix,$14,prefix,$15,prefix,$16,prefix,$17)}' >> $tempfile
tempfileSize=$(stat -c%s $tempfile)
echo Starting Perl Script
randomColour e
cd ../
cat $tempfile | QUERY_STRING="Folder=$theDirectory&action=doProcessUploads" CONTENT_LENGTH=$tempfileSize perl -T index.cgi 2>&1 | sed -u -n '/NEW IMAGE/ {s/\(.*\)IMAGE\(.*\)/Perl looks like it is processing image\2/p}' | everyLineADifferentColour
cd ../../upload/
randomColour e
echo 'Hope you enjoyed the ****** Image Resizer Experience!'
tput setaf 0
tput sgr0