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...

Let's Script!

 So now that we've got all that basic stuff out of the way, let's get into
some serious shell scripting.

    You know how to string commands together by now; you just create a text
file, start listing any commands you want to execute, save the file, change
its permissions (chmod +x <filename>) and viola, you have a shell script.

    But what if you want to do something more? What if you want to pass
parameters on the command line? What if you want to check for files
existence before executing a command? What if you want to compare the values
of two variables?

    What indeed? You will need to know a bit about Loop Constructs and
Conditional Constructs.

   Loop constructs are:

   Conditional constructs are:

    I will list the syntaxes of each and give basic real life examples to
help you along your way. None of these are exhaustive, but they will give
you a good foothold to allow you to further research bash and learn how to
kick serious butt.

    One thing to remember with the following constructs is that they are not
like most other languages you may be used to. When you do a test, you are
actually executing commands. Whether these are commands built-in to bash, or
whether they are other commands it doesn't matter, but you can't get away
with direct comparisons of variables. You'll see in the examples. Also, all
semi-colons (;) can be replaced with newlines.


   bash is VERY space sensitive. Unless you copy any example scripts
   EXACTLY, you will most likely encounter problems.

      Syntax:           until <test>; do <commandlist>; done
      Description:      <commandlist> will continue to be executed until <test>
                        has an exit status which is not zero (ie; until it's not
      Example script:

            until [ "$counter" = "10" ]
               echo $counter
               let counter+=1

    This script is very simple. It sets a variable called "counter" and
initializes it to a value of 1. (We don't need to export it as it doesn't
need to be in the environment. Only this script will be using it.)

    Then we want to echo a list from 1 to 10, so we do the comparison. We
put the test arguments into square brackets so that bash knows we are doing
a comparison. It will keep looping until the variable counter reaches a
value of "10".

    The line "let counter+=1" is saying "take the contents of the variable
counter, add 1 to it, and save the value back into counter. We could have
also typed:

let counter=$counter+1

   Note where you use the $ preceding the variable names and when you don't. When we want to display the actual value in the variable we use $variable. When we want to reference the variable for the reasons of changing it, we just use variable.

    The 'let' command allows you to do mathematical equations.
Alternatively, you can wrap the equation in $[]. For the example above, it
would be:


    This is know as Arithmetic Expansion, and I have found it to be less
reliable than using the let command. Also, using let makes your scripts that
much more readable in the future.

More on bash --->> 

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.