The Bash Shell,continued...
So you know what sort of mathematical operators
you can use, here is a list:
unary minus and plus
logical and bitwise negation
* / % multiplication,
left and right bitwise shifts
<= >= < >
== != equality
bitwise exclusive OR
= *= /= %= += -= <<=
>>= &= ^= |=
while <test>; do <commandlist>; done
do <commandlist> while <test> has an exit status
of zero. (ie; true)
while [ "$counter"
!= "11" ]
We use the same script here using a different
method of testing. This
script will continue to loop while the variable $counter is not
means that it will happily process the commandlist until $counter
Note that this is a string comparison, not an integer (numerical)
for <var> [in <list>]; do <commandlist>; done
This will execute <commandlist> for each item in
If the optional [in <list>] is left out, bash
will replace this with $@.
for i in cheese
comics "computers which don't suck" motorbikes
echo "I like $i"
This will display:
I like cheese
I like comics
I like computers which don't suck
I like motorbikes
As you can see, each word is treated as
a separate parameter unless you
use quotes to block a group of words.
if <test>; then
elif <test2>; then
elif <test...>; then
Description: This command is
rather more complex in nature. Using elif,
you can include as many tests as you like. If the script fails
to find a match in any of the tests, it will default to executing
the commandlist under else.
The <test> has an extensive list of parameters you can
pass which allow you to test for various things including whether
files exist on your hard drive, comparing 2 variables, etc.
if [ -z $1 ]
echo "enter your favorite band name"
if [ "$1"
= "manowar" ]
echo "the greatest band on the planet!"
elif [ "$1"
= "DIO" ]
echo "awesome! they rock!"
elif [ "$1"
= "KISS" ]
echo "ah. that's taking me back..."
echo "never heard of $1"
First, if you don't know it already, $1
indicated the first parameter on
a command line. For example, if you enter 'cd /home/me', "/home/me"
first parameter that you have entered. Inside the script, or
the cd command,
it could be referred to as $1. $2 is second, $3 is the third,
etc. One nice
ability is to use $0, which refers to the calling command - in
"cd." Even batter is $* which stores all command line
parameters, except $0.
OK, next we introduce two kinds of tests
in the if construct. -z checks
to see if the user has entered any parameters, and promptly informs
when they forget to do so.
The next one which is used in the rest
of the script compares two
variables. Both are wrapped in quote marks to avoid bash trying
them as commands.
Another point to note is make sure to separate
the square brackets,
otherwise bash will get confused.
Other useful if lines:
if [ -f ~/.profile ]
(checks to see if you have a .profile file in you current home
If so, the test will pass TRUE)
if [ ! -f ~/.profile ]
(checks to see if ~/.profile is NOT there and return TRUE if
file is NOT there.)
Take note, this only checks for the existence
of files, not directories.
Can you figure out how to make it check for the existence of
case <var> in
pattern [| pattern]) <commandlist> ;;
pattern [| pattern]) <commandlist> ;;
The case command firstly expands whatever is in
<var> and compares it to all the patterns listed. If it
finds a match, it
will execute the appropriate <commandlist>. More
than one pattern can be
included per line by separating them with a pipe (|) symbol.
Also, you can use the pattern * to include everything
else. Example script:
case $1 in
cheese | bread | meat)
echo "food!" ;;
viper | stingray | transam)
echo "car!" ;;
echo "I don't know what it is!" ;;
This is fairly straight forward once you
get to grips with it. It pulls
in a parameter that you enter on the command line and tries to
what it is. If you enter "cheese", "bread"
or "meat", our clever script will
recognize this as a type of food and will tell you so.
If you enter "viper", "stingray"
or "transam", it will recognize it as a
car! Anything else leaves our poor program clueless.
There is also a command called select which
was stolen from the korn
shell. The syntax for this is very similar to case. See if you
can figure it
out and get it working. HINT - read "man bash".
There are many more features of bash, I
have not covered a great
portion, but that is part of the fun. Now go read the man page
should help you make sense out of it!) and see what other nifty
features bash has.
I wrote this tutorial using bash version 1.14.7(1).
I am about to upgrade
to 2.02.01 or whatever the latest is to see what differences
that gives me.
Get the latest bash version via ftp at
prep.ai.mit.edu or one of its
mirror sites. This is a good time to brush up your search engine
Where are those back issues of GTMHHs and Happy Hacker Digests?
Check out the official Happy Hacker Web page at http://www.happyhacker.org.
We are against computer crime. We support good, old-fashioned
hacking of the
kind that led to the creation of the Internet and a new era of
information. So don't email us about any crimes you may have
© 1998 idle (email@example.com).
You may forward, print out or
post this GUIDE TO (mostly) HARMLESS HACKING on your Web site
as long as you leave this notice at the end.