We’re creating a Korn Shell program today for use with the Mac OS X command line, or for use with Linux or unix, in posing the following individual file restore or recovery scenario …
#! /bin/ksh
# copy_depending.ksh
# If filesize too small and the filesize of something else okay and filesize of first is
# less than minbytes copy second onto first ksh script tutorial
# Possible usage is that parameter two file is a backup of parameter 1 file and in the
# meantime a bad change happened to file 1 causing it to become smaller than
# parameter 3 minimum bytes setting … this script gets it back to the way it was
# as the backup happened
if [ ! -f "$1" -o ! -f "$2" -o -z "$3" ]; then
echo "Usage: copy_depending.ksh [file1] [file2] [minbytes] # if filesize too small and\
the filesize of something else okay and filesize of first is less than minbytes\
copy second onto first";
else
size1="`stat $1 | cut -d' ' -f 8`";
size2="`stat $2 | cut -d' ' -f 8`";
b="`basename $1`"
if [ $size1 -lt $3 ]; then
if [ $size1 -lt $size2 ]; then
if [ $1 -nt $2 ]; then
msg="New $1 from $2 (backup, perhaps) and will email contents of the clobbered $1";
cat "$1" | uuencode "$b" | mailx -s "$msg" rmetcalfe15@gmail.com;
cp -f $2 $1
fi
fi
fi
fi
exit
… oops … that’s all of it (copy_depending.ksh) today … better be off then … no … want to explain a bit in color coded terms.
- Today’s Shebang points at Korn Shell usage and makes ./copy_depending.ksh work but please read the chmod bit further below to complete this knowledge
- Command line parameters usually get arranged so that $0 points at the script itself, $1 is first parameter, $2 is second etcetera
- This is checking for the non-existance of what follows
- Linux mailx Primer Tutorial explains this below
- The stat command is what we use to extract the filesize information from the eighth column
- The cp command is what we use to restore the file, should it come to that
- This is checking if $1 file is newer than the $2 file
Today’s tutorial picture shows you a simulation of this Korn Shell script’s usage. In order to be able to go …
./copy_depending.ksh you_only_read_once.html you_only_read_once.html_backup 1500
… during this session we had to, ahead of time, make sure the execute bit was set for the relevant user and we did this via …
chmod 755 copy_depending.ksh
Perhaps this could be the (basis for the) backup to the backup you need.
Previous relevant Linux mailx Primer Tutorial is shown below.
Think Linux and you may not associate it with email, but Linux can be excellent for many email requirements, especially using mailx and uuencode. The use of uuencode allows you to give your email an attachment, and the mailx -s switch for the subject of the email can be used for both the purpose it was meant for, that is to establish an email subject, and with a stretch of imagination of usage, the body text, because of the quirk whereby anything after the first line of the subject will spill over into the body text of the email. So, am sure there would be limits to the length of this body text, but you can often be brief when there is a good attachment to provide your email recipient. Naturally, in the day and age of worry about computer viruses, your attachment will most likely be vetted by the receiving email client software for viability, so be aware of this. Other than all that, this method of sending emails is potentially very powerful. Notice that great way Linux and Unix can use `cat body.txt` type of syntax to embed one command within another … sometimes Linux is so simple, powerful, brilliant! You may have noticed how short all the commands in Linux and Unix tend to be, and that is because it was intended to be short and powerful, and this is brilliant.
Background reading for tutorial:
Here is a tutorial that introduces you to some email with attachment, subject and body text from the Linux command line.
If this was interesting you may be interested in this too.
If this was interesting you may be interested in this too.