Programming Style
This script example is intended to be an actual practical script for making
pupsave backups.
The primary reason I may not have a current backup is because of
convenience factors. If I make backups very convenient or even
automatic, I will have current backups.
I often don't follow Linux conventions in scripting. Part this is C
conventions which I learned to like. In C, variables are conventionally
lower case and structure is function based. The way I write a program is
by making small functions. (It is easier for me and easier to test and
debug). Also, I like to use sufficient white space to make things
more readable.
The script below has been tested, I'll attach a zip file soon. I kept
everything to a character length of less than 80. To do this I used a \
which tells Bash to continue on to the next line as part of the command.
It is extremely important there be no white space following the \
otherwise, the script won't even work. Also, one reason for the scripts
posted above by me and wonderfully polished by Shep, is to remove
trailing white space.
Note the function names tend to serve as a comment giving a hint about
what the function is for.
Here is my function based script.
Code: Select all
#!/bin/bash
main() {
variables
sanity
already_bked_up
clear_pupsave
make_bkup
exit 0
}
variables() {
datestamp=`date "+_%Y_%m_%d"`
mode=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPMODE \
| cut -d = -f 2`
pupdir=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPSAVE \
| sed "s/'//g" | tr "," " " | cut -d " " -f 3 | cut -d "/" -f 2`
pupfile=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPSAVE \
| sed "s/'//g" | tr "," " " | cut -d " " -f 3 | cut -d "/" -f 3`
script=`basename $0`
xchck=`pidof X`
devsave="/initrd/mnt/dev_save"
for i in `echo $pupfile | tr "." " "` ; do
ext=$i
done
pupbasefn=`basename $pupfile .$ext`
}
sanity() {
[ ! -d /initrd/pup_rw ] && echo "This is not a Puppy Frugal install, \
goodbye" && exit
[ "${#xchck}" -gt "1" ] && echo "X is running, goodbye" && exit
if [ "$mode" != "12" ] ; then
echo ; echo "$script has only been tested with Puppy's PUPMODE 12,"
echo "due to lack of testing on other PUPMODES,this opeartion"
echo "is terminated"
echo ; echo "Recommendation,do your own testing and refining of"
echo "$script for other PUPMODES" ; echo
exit
fi
}
already_bked_up() {
if [ -f $devsave/$pupdir/${pupbasefn}$datestamp.zip ] ; then
a=N
echo "You already have a backup for today"
echo -n "Do you want another backup (y,n)? "
read -n 1 a
if [ "$a" = "y" ] ; then
for i in {a..z} ; do
if [ ! -f $devsave/$pupdir/${pupbasefn}${datestamp}_$i.zip ]
then
datestamp="${datestamp}_$i"
break
fi
done
else
echo ; exit 0
fi
fi
echo
}
clear_pupsave() {
echo "Clearing free space in $pupfile, please wait..."
dd if=/dev/zero of=/initrd/pup_rw/zeros.tmp bs=1M
rm /initrd/pup_rw/zeros.tmp
}
make_bkup() {
echo "Making $devsave/$pupdir/$pupbasefn$datestamp.zip"
zip $devsave/$pupdir/$pupbasefn$datestamp.zip $devsave/$pupdir/$pupfile
ls -l $devsave/$pupdir/$pupbasefn$datestamp.zip
}
main
Here is the identical script without functions, with uppercase variables and
no extra white space.
Code: Select all
#!/bin/bash
DATESTAMP=`date "+_%Y_%m_%d"`
MODE=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPMODE \
| cut -d = -f 2`
PUPDIR=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPSAVE \
| sed "s/'//g" | tr "," " " | cut -d " " -f 3 | cut -d "/" -f 2`
PUPFILE=`cat /initrd/pup_rw/etc/rc.d/PUPSTATE | grep PUPSAVE \
| sed "s/'//g" | tr "," " " | cut -d " " -f 3 | cut -d "/" -f 3`
SCRIPT=`basename $0`
XCHCK=`pidof X`
DEVSAVE="/initrd/mnt/dev_save"
for I in `echo $PUPFILE | tr "." " "` ; do
EXT=$I
done
PUPBASEFN=`basename $PUPFILE .$EXT`
[ ! -d /initrd/pup_rw ] && echo "This is not a Puppy Frugal install, \
goodbye" && exit
[ "${#XCHCK}" -gt "1" ] && echo "X is running, goodbye" && exit
if [ "$MODE" != "12" ] ; then
echo ; echo "$SCRIPT has only been tested with Puppy's PUPMODE 12,"
echo "due to lack of testing on other PUPMODES,this opeartion"
echo "is terminated"
echo ; echo "Recommendation,do your own testing and refining of"
echo "$SCRIPT for other PUPMODES" ; echo
exit
fi
if [ -f $DEVSAVE/$PUPDIR/${PUPBASEFN}$DATESTAMP.zip ] ; then
A=N
echo "You already have a backup for today"
echo -n "Do you want another backup (y,n)? "
read -n 1 A
if [ "$A" = "y" ] ; then
for I in {a..z} ; do
if [ ! -f $DEVSAVE/$PUPDIR/${PUPBASEFN}${DATESTAMP}_$I.zip ] ; then
DATESTAMP="${DATESTAMP}_$I"
break
fi
done
else
echo ; exit 0
fi
echo
echo "Clearing free space in $PUPFILE, please wait..."
dd if=/dev/zero of=/initrd/pup_rw/zeros.tmp bs=1M
rm /initrd/pup_rw/zeros.tmp
echo "Making $DEVSAVE/$PUPDIR/$PUPBASEFN$DATESTAMP.zip"
zip $DEVSAVE/$PUPDIR/$PUPBASEFN$DATESTAMP.zip $DEVSAVE/$PUPDIR/$PUPFILE
ls -l $DEVSAVE/$PUPDIR/$PUPBASEFN$DATESTAMP.zip
exit 0
These scripts are much easier to work with using a text editor with syntax
highlighting.
Bruce
Any comments or questions?
~