What's New!

Chat with

How to Defend
Your Computer 

The Guides
to (mostly) 
Harmless Hacking

Happy Hacker 
Digests (old stuff) 

Hacker Links 


Meet the 
Happy Hacksters 

Help for 



It Sucks 
to Be Me!

How to Commit
Computer Crime (not)! 

What Is a 
Hacker, Anyhow? 

Have a 
Great Life! 

News from the 
Hacker War Front

The Bash Shell,continued...

Mathematical Operators:

   So you know what sort of mathematical operators you can use, here is a list:

      - +      unary minus and plus

      ! ~      logical and bitwise negation

      * / %    multiplication, division, remainder

      + -      addition, subtraction

      << >>    left and right bitwise shifts

      <= >= < >  comparison

      == !=    equality and inequality

      &        bitwise AND

      ^        bitwise exclusive OR

      |        bitwise OR

      &&       logical AND

      ||       logical OR

      = *= /= %= += -= <<= >>= &= ^= |=
               assignment operators

      Syntax:           while <test>; do <commandlist>; done
      Description:      do <commandlist> while <test> has an exit status
of zero. (ie; true)
      Example Script:

         while [ "$counter" != "11" ]
            echo $counter
            let counter+=1

    We use the same script here using a different method of testing. This
script will continue to loop while the variable $counter is not 11. This
means that it will happily process the commandlist until $counter equals 11.
Note that this is a string comparison, not an integer (numerical) comparison.

      Syntax:           for <var> [in <list>]; do <commandlist>; done
      Description:      This will execute <commandlist> for each item in
                        If the optional [in <list>] is left out, bash
will replace this with $@.
      Example Script:

         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.

      Example script:

         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" is the
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 this case
"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 them
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 to execute
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 dir.
If so, the test will pass TRUE)

      if [ ! -f ~/.profile ]
(checks to see if ~/.profile is NOT there and return TRUE if the
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 directories?

      Syntax:           case <var> in
                           pattern [| pattern]) <commandlist> ;;
                           pattern [| pattern]) <commandlist> ;;
      Description:      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 figure out
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 (this text
should help you make sense out of it!) and see what other nifty little
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 skills ;)

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 freedom of
information. So don't email us about any crimes you may have committed!

© 1998 idle (idle@mailexcite.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.

Carolyn's most
popular book,
in 4th edition now!
For advanced
hacker studies,
read Carolyn's
Google Groups
Subscribe to Happy Hacker
Visit this group


Return to the index of Guides to (mostly) Harmless Hacking!

 © 2013 Happy Hacker All rights reserved.