Bash programming flow
The lines are read from top to bottom and executed in that order.
Often we write our programs line1, line2, line3 and etc.
When done, if we did it right, our program works.
We write a program in order to do something for us. In most cases I write
programs because I want to solve problems. The problem might be how
to make something complex - simple.
We find in writing good programs we want to implement conditional check
and branches. Otherwise we can write a destructive program.
It is very important to know exactly what you want your program to do,
before writing it.
Think about it for a while and write notes before you program.
I'll make a demo program to show you how to make conditional checks
and branches.
Progname: bkuproot
Purpose: to make a backup of /root files on a separate partition
I need to:
make sure the partition is mounted
that there is sufficient space on the partition
I'll use the zip utility for my backup
I'll skip the space check to make my example easier
zip will bomb out with an on screen error if there wasn't enough space
My primary purpose here is actually to demonstrate how to make a
conditional check and branch, this should be easy. I'm ready to code.
Code: Select all
#!/bin/bash
DESTPART=/dev/sda2
DESTDIR=rootbkup
FILENAME=bkuproot.zip
Please note: by using variables, we make our script easily portable, and
we can make them easy to modify. We merely change the data in the
variable. We don't have to search the entire script.
Let's first make sure /dev/sda2 is mounted
Code: Select all
</proc/mounts grep $DESTPART>/dev/null
/dev/null is Linux' black hole, everything we redirect to /dev/null disappears.
By redirecting the output to /dev/null we eliminate all on screen output.
Our search criteria is /dev/sda2
Grep will not tell us if /dev/sda2 is mounted. It tells us if it found our
criteria. If it found our criteria, we know it is mounted, because grep will
give us an error code of 0 if it found the text.
What we are going to test for is grep's error code. The error code is found
in the variable $? If grep found the text then we have error 0
Now we introduce the first part of an if statement.
Code: Select all
if [ "$?" = "0" ] ; then
[ ! -d $DESTPART/$DESTDIR ] && mkdir $DESTPART/$DESTDIR
See how by using variables we can change the file merely by modifying
the easy to find variables at the top of the file. No searching and replace
necessary.
-d means we are looking for a directory
If a backup file already exists, let's delete it
Code: Select all
[ -f $DESTPART/$DESTDIR/$FILENAME ] && rm $DESTPART/$DESTDIR/$FILENAME
-f says we are looking for a file
Now let's make the new backup file. Note: many users make their
backups by putting dates in the file names. For our purpose, I'll keep it
more simple.
Now we are ready to make our backup
Code: Select all
zip -r9y $DESTPART/$DESTDIR/$FILENAME /root
We don't have to check for the existence of /root, if it doesn't exist, we
have serious problems to work out.
zip is the command
-r9y means recurse /root, use maximum compression and preserve links,
don't convert to the files they reference
When you write your script you have to learn about the utility you want to
use, in this case zip
zip --help will show its options
Here is our safety branch:
Code: Select all
else
echo "$DESTPART is not mounted, no changes made"
we terminate our if statement like this:
Now I'll put it all together, see if you can read it and understand it
Code: Select all
#!/bin/bash
DESTPART="/dev/sda2"
DESTDIR="rootbkup"
FILENAME="bkuproot.zip"
</proc/mounts grep $DESTPART>dev/null
if [ "$?" = "0" ] ; then
[ ! -d $DESTPART/$DESTDIR ] && mkdir $DESTPART/$DESTDIR
[ -f $DESTPART/$DESTDIR/$FILENAME ] && rm $DESTPART/$DESTDIR/$FILENAME
zip -r9y $DESTPART/$DESTDIR/$FILENAME /root
else
echo "$DESTPART is not mounted, no changes made"
fi
Most of my scripts look like a C programmer who escaped from a mental
ward who is trying to write in bash.
This time I think I actually followed bash conventions.
Namely using uppercase variables, quoting the variable data, and
making the script easily changed and portable.
If our partition is not mounted we have a false condition at the
beginning of our if statement. No commands execute. if looks for else and
executes it which is merely an on screen message to the user.
Also, notice the indents, they tell us what commands run within the
statement. It makes the code easier to work with and read. Especially
helpful when statements are nested and very long.
By the way this program has not been tested. If anyone can find errors,
I'll agree to owe them a nickel.
EDITED: You guys and gals are not reading well. The program contains a serious error. Please find it so you can be owed a nickel.
~
~