BASH scripting in Linux: an introduction [Linux]

I've already used a bit of BASH scripting in my Wifi sniffing tutorial, but the importance of scripting in BASH and other languages such as Perl, Ruby, and Python is so great I need to write separate posts for them all.
Bash stands for "Bourne-Again Shell" (you will see "sh" stands for "shell" in many places). Named aptly for being the successor of the Bourne Shell, it came into use in 1989 and has since been a main scripting language for Linux and has many different options such as piping (seen before on my blog), variables and control structures (like all good languages), file reading, and the Unix "wildcard" usage by the asterisk (*) key.

Enough about stuff I'm sure you guys don't care about, lets jump right in!
First, I'll start off with some basic BASH variables and interesting things you should know before attempting to write your own program, then I'll go over basic programming syntax in a BASH environment and show some examples.

In Unix as well as MSDOS and Windows, there are variables (I'm sure we all know what variables are) called "environment" variables, and they deal with certain processes running and affect those processes in many ways. Here's a short list of important ones:

PATH - lists directories the shell searches, for the commands the user may type without having to provide the full path.
HOME - indicates where the user's "home" directory is located in the file hierarchy.
USER - indicates the current user. Try running the command "echo $USER" and viewing the output; it should be your login.
TERM - specifies the type of terminal being used by the user.
PS1 - specifies how the prompt is displayed in the shell or terminal while the system waits for a command. Mine on Backtrack5 is ">" but for some it is "$" this can be changed to be anything (for instance, your current directory for easy knowledge).
PS2 -  specifies how the prompt is displayed in the shell or terminal while the system waits for more input; it is like the PS1, but instead of when there is no command running, this is for when a command or process is waiting for more input.
MAIL - used to indicate where a user's mail is to be found.
TEMP - location where processes can store temporary files while running a script or process.
More will be added, but this is a good list of some important ones you can use for now. Try Googling with the Google search above if you want to find out more about environment variables in Linux/Unix.

Now before starting your scripting career in BASH, there are some important things you need to remember. The first is that one should ALWAYS ALWAYS ALWAYS start their script file with #!/bin/bash (which you will hear programmers refer to as "hash bang").
This tells the Unix environment what type of shell you're using (bash in this case), and the location of the shell.
Have this set as your first line and keep a couple blank lines in between this and the actual code so you realize where this is. It should ALWAYS be first. I think you get the importance of this.
For future note when I write my Perl tutorial, the first line in a Perl (.pl) script must be "#!/usr/bin/perl"

When you make your script file, whether it be through Nano or another text editor such as Vi/Vim, I like to have the format in  the format: "" where the ".sh" tells us it's a shell script file. You can name it mostly anything, but it's quite a bit easier keeping it ".sh" so later if you're using the ls command or whatnot, you can search for all your shell scripts!

After we've created the file and added in the #!/bin/bash line, we need to make this executable by the system. To do this, we type the command "chmod +x"
What this does is adds to the file the access of executable (the plus sign adds, and the x stands for executable). After we've done this, we can run the file by typing a few different commands.
You can either type "./" or "sh" or "bash" to run it. The first requires you to be in the same directory, however.
Other options you can add are "r" and and "w" which stands for read and write respectively. You can add and remove these privileges by typing +rwx or -rwx depending on which you want. You can also use numbers to differentiate what privleges you want.
Instead of my reiterating this topic, here's an excellent and short website that explains it.

If->Then conditional statements:

I'm sure most of you have programmed before, whether it be in C++, Java, or some other language and are somewhat aware of how to use if then statements, but for those who aren't, I'll explain what they are quickly and for everyone the syntax (which means formation) of them in BASH.

If then statements are pretty easy to understand since they're named aptly for their use. They are used as a conditional statement (meaning, it tests a condition) and depending on the return (whether or not it is true or false) it executes (or runs) a certain line or lines of code that you choose.

The syntax of if then statements in BASH is as follows:

if [condition]; then
     expression if true
     expression otherwise if false
fi # ends if statement

The ending "fi" is necessary for BASH to tell the computer where your if statement starts and ends. "fi" is of course "if" backwards.
You can have an if then statement with only one set of expressions (it doesn't require an "else" part), or as many "elses" as you want, but you HAVE TO end with the "fi" line.
Depending on how many elses you wish you add, there are many other ways to do this in an easier fashion that I will cover later, as well.
For Loops:

Now again, if you've had any experience with programming, for loops shouldn't be anything new to you; however, for loops in BASH have a little different of a syntax. This time, I'm going to go over the syntax of a for loop in BASH, then explain the uses for our new readers and how these are one of the most important aspects of programming.
In BASH, there are a few ways to do for loops, which is interesting because in most programming languages there's one basic syntax. Here are a few ways to do them.

My favorite way to do for loops is this:

for var in {1..10}
     echo "the variable var is $var"
done # closes for loop

If you're used to Java or C++ or another high level programming language and their for loops, you can use this syntax:

for (( i=0; i<10; i++ )) # note that spaces are a MUST (BASH is weird like this)
     echo "increment variable i is $i"
done # closes for loop

You can add a "break" command inside the loop, which I would recommend throwing into an "if then" statement for error or input checking.
As well as the break command, you can have a "continue" command which automatically skips to the next iteration; meaning if i is equal to 5, but whatever  you want has already been completed, you can have an if statement check your needs then simply add the "continue" statement and it will go to the 6th iteration.
This gets into bigger and better scripts in BASH and can be used quite effectively depending on your scripting needs.
While Do Loops and Do Until Loops:

Another basic and important programming syntax to understand is Do->While loops, which can be either do while or do until. I'll explain both and their uses.

The basic while loop syntax is as follows:

while [ expression ]; do
<< Block >>

Note that the squared parenthesis must have spaces between the tested expression or it won't compile and run.

A do until loop does the block statement UNTIL the expression evaluates to true, which is the opposite of the while do loop.
Here is an example:

until [ expression ]; do
<< Block >>

So if you have the expression "i = 0" and the variable is prompted every time the loop goes through, if we used a while loop, the variable would only continue if the use input "0" every time, but if we used an "until," the  loop would continue UNTIL i equals 0, meaning that ANY other number would continue the loop other than 0.

Popular posts from this blog

[OLD] So you want to use Backtrack 5? [With Pictures/Windows/Mac/Linux]