Tidying archived install kit files.
I've finally (two years later!) got round to creating the final pieces of the mozupdate script and am now awaiting new releases from Mozilla so that I can complete UAT on the changes.Changes made are...
- Add an exit trap to remove a work file the script now creates in the /tmp directory.
- Add a simple check to ensure that the user provided the required parameter when invoking the script.
- Add a new function to action the archive clean up.
- Add a small piece of code to ask the user if they want to remove older archived files.
Exit trap
On exit, the shell executing the script will be sent an EXIT signal. This is your last chance to perform any required actions on termination of your script (although only non-blocking actions should be performed as there is not an infinite amount of time available).Setting a signal trap is straightforward - the command syntax is basically
trap command SIGNAL_NAME. Always quote command and use ';' to separate multiple commands.
Here's the exit trap I've added to the script...
# set an exit trap to clean up any work files
trap 'rm /tmp/*.$$ 2>/dev/null' EXIT
Note the '.$$' appended to the file matching pattern: $$ is substituted with the shell's PID number by the shell which provides a convenient way of identifying files that belong to this invocation of the script.
Check parameter present
Just a trivial check to ensure that a parameter (which needs to be the absolute pathname of the file containing the Mozilla install kit) has been given# check some sort of parameter given
if [ "$1" = "" ]
then
ABORT absolute pathname of mozilla install kit is mandatory
fi
Enough said.
Archive Clean-up Function
This is the nuts and bolts of the archive clean-up. The first rough cut of this had control structures (if, for, while, until, case) nested 4 deep and needed some simplification.I decided to use a count of the number of install kits to retain in the archive rather than attempt to discard by age, which is tricky when releases happen at arbitrary intervals. This count is set in a global variable at the start of script execution. A sensible minimum is 2, so that you can reinstall the current and previous versions if the installation directories get damaged.
arcmax=6 #max number of install kits to keep
The main function performs it's actions in two stages: firstly generate a list of candidate files to remove in a temporary file (/tmp is a good place to put these) sorted newest first and then attempt to remove all these files except the first arcmax listed.
# define function to delete all but the most recent arcmax installation kits
# from the archive directory.
function CLEAN_ARCHIVE {
typeset -i i #create local variables i and arcfile
typeset arcfile #used in this function only.
# get list of candidate files for removal
if [ -d "$kitarc" ]
then
#one filename per line sorted newest first
#file created here will be removed by exit trap defined earlier.
ls -1 -t $kitarc/$mozprod* >/tmp/arclist.$$
else
echo Can\'t access archive directory $kitarc
return 1
fi
# Remove all but the first arcmax files
i=0
while read arcfile
do
i=$((i+1))
[[ i <= arcmax ]] && continue #skip first arcmax entries in list
rm "$arcfile" && echo removed "$arcfile"
done </tmp/arclist.$$
return 0
}
Note there are now no nested control structures and the function is comparatively simple. The only slight oddness is the use of a free-standing test followed by the conditional execution operator &&. This works because the test operator has to return it's result in $? (the return code of any executable) or it wouldn't work with an if statement. In any loop body delimited by 'do' and 'done' continue means 'scrap any further instructions in this iteration and go round again' and break means 'scrap any further instructions in this iteration and continue execution from the next instruction after 'done' thus scrapping any further iterations of the loop.
User Interaction
Asking users questions keeps them awake and stops them getting bored. Here's the little scrap of code that asks the user if they would like to tidy the archived collection of install kits.# Remove older archived install kits if required.
if ASKYN Remove older versions of $mozprod from archive directory
then
CLEAN_ARCHIVE
else
echo Not removing older archives of $mozprod
fi
Nice and simple. This happens right at the end of the mainline code, so the only thing that happens afterwards is a goodbye message and a return to the command prompt.
ASKYN is listed in the text of the script in an earlier post.
That's it for this post. All I need now is a firefox or thunderbird update so I can complete final testing of the changes.
No comments:
Post a Comment