What's New!

Chat with
Hackers

How to Defend
Your Computer 

The Guides
to (mostly) 
Harmless Hacking

Happy Hacker 
Digests (old stuff) 

Hacker Links 

Hacker
Wargames 

Meet the 
Happy Hacksters 

Help for 
Beginners 

Hacker 
Bookstore 

Humor 

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

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

         --------
         #!/bin/bash
         counter=1
         while [ "$counter" != "11" ]
         do
            echo $counter
            let counter+=1
         done
         --------

    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.

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

         --------
         #!/bin/bash
         for i in cheese comics "computers which don't suck" motorbikes
         do
            echo "I like $i"
         done
         --------

    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
      Syntax:
                        if <test>; then
                           <commandlist>
                        elif <test2>; then
                           <commandlist>
                        elif <test...>; then
                           <commandlist>
                        else
                           <commandlist>
                        fi

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:

         --------
         #!/bin/bash
         if [ -z $1 ]
         then
            echo "enter your favorite band name"
            exit
         fi

         if [ "$1" = "manowar" ]
         then
            echo "the greatest band on the planet!"
         elif [ "$1" = "DIO" ]
         then
            echo "awesome! they rock!"
         elif [ "$1" = "KISS" ]
         then
            echo "ah. that's taking me back..."
         else
            echo "never heard of $1"
         fi
         --------

    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?
 

   CASE
      Syntax:           case <var> in
                           pattern [| pattern]) <commandlist> ;;
                           pattern [| pattern]) <commandlist> ;;
                        esac
      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:

         --------
         #!/bin/bash
         case $1 in
            cheese | bread | meat)
               echo "food!" ;;
            viper | stingray | transam)
               echo "car!" ;;
            *)
               echo "I don't know what it is!" ;;
         esac
         ---------

    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
Email:
Visit this group

 

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

 © 2013 Happy Hacker All rights reserved.