In this lesson we're going to talk about coding in Bash.
Now Bash is the first one we're going to cover
because it comes installed by default inside every
Linux distribution you're going to come across.
Bash is a command line scripting language
that's used for the command shell inside Unix-like systems,
including Linux and Mac OS.
Now if you're familiar with Linux
and you've done some command line programming before,
you're probably really used to using the Bash shell
to be able to execute those commands.
As a scripting language,
Bash is extremely useful when you're automating tasks
that use command line environments inside of Unix.
If you want to be able to call things like Nmap,
for example,
you can do that using Bash shell scripts,
because they already know how to interact with those
different tools that are installed on your Linux system.
The one big limitation when scripting in Bash
is that it is not an object oriented language.
So you are a little bit more limited when you're using Bash
for scripting than you are in other languages like Python,
Pearl,
Ruby,
PowerShell,
or JavaScript.
Now in this lesson we're going to go through some basic Bash,
syntax and commands that you may come across
as you're working with Bash scripts.
First,
anytime you start looking at a Bash script,
it should always start out with the same line.
And this is hashtag bang slash bin slash bash.
This means you're going to see that hashtag,
which normally signifies a comment line
and then the bang or exclamation mark,
then slash bin slash bash,
which says this is a Bash script
and it should be executed using the Bash shell.
From there you're then going to have whatever else you want
coded into that script for what you want to do.
Now if you're looking at a script
and you're trying to figure out if it's a Bash script,
what's the first thing you should look at?
That's right,
you should look at the first line.
If it has hashtag bang slash bin slash bash,
you know it's a Bash script.
Now any other time you see that hashtag
you should remember that this is a comment.
Now this holds true in many different programming languages,
Bash,
Python,
Ruby,
and PowerShell all use the syntax of a hashtag
to signify everything beyond that hashtag
is going to be ignored by the scripting engine or compiler
because it's just a human readable comment.
So I might say,
hashtag,
this is the first line of my script.
And that just tells people that that's
the first line of my script,
or I might tell them exactly what this script is used for.
Maybe it's hashtag this script is used to do backups
of my systems.
Something like that.
The next thing we need to talk about inside of Bash
is the idea of variables.
Now we've talked generally about variables before
and how they're used to represent any value
and that they can be changed during the execution
of the program.
For Bash you can declare a variable by using the variable
name equals and the thing you want it to be.
For example,
if I did customer name equals Jason,
that would now define that variable customer name
as a variable.
Now anytime I want to call that customer name variable,
I'm not going to write customer name,
instead I'm going to use dollar sign customer name
and the dollar sign customer name says,
I want to call the variable known as customer name.
So when you're assigning something,
there's no dollar sign.
When you're calling it,
there is a dollar sign.
Also notice that I use a syntax
of uppercase and lowercase letters here,
generally in Bash and a lot of our other naming schemes,
we will call things as variables using title case
where the first letter is capitalized
and the rest of the word is lowercase.
This just makes it easier to read
when I have double name words,
like customer name,
being used as a single variable.
In Bash when you declare your variable,
you can also type that variable.
Now typing is where you declare a specific function
essentially for that variable.
Is it going to be a string,
a numeric array,
a number,
or something like that.
Essentially to do this,
you're going to do declare option the variable name
equals the value.
So for example,
if I wanted to create a variable called phone number
and I wanted it to be treated as a number,
I can declare it by saying,
declare dash I then phone number equals
and whatever value I want like 1, 1, 1, 1, 1, 1, 1.
This is now my seven digit phone number
and it will be treated as a number.
Now if you want to make something a constant
where it's not going to change,
such as the definition of Pi,
we can do that by again using the declare option.
We're going to do declare dash r Pi equals 3.14.
In this case,
I made that variable read only,
or in other words,
it became a constant because I can't change it again.
The next thing we need to understand is how to use arrays
inside of Bash.
Now if I want to create an array in Bash,
I'm simply going to give it a name like I would a variable.
So I might use something like tempArray equals
and then I want to put all of the things it's going to equal
inside of parenthesis and separate it by commas.
So tempArray equals parenthesis value1 comma value two
comma value three.
Now I have an array with three different items in it
listed as value one, two, and three.
If you want to get information stored in an array
back out inside of Bash,
you're simply going to refer to it as dollar sign,
the array,
bracket and the position number.
So in the case of my temporary array with my three values,
if I wanted to pull out value two,
I would use dollar sign,
tempArray bracket one end bracket.
This would then be able to give me the value
from position number two because we count from zero,
then one,
then two inside of computers,
and that would give me value two back as the thing
I'm referencing.
Next we have named arrays and associative arrays.
this almost like a table inside of a database.
And any data stored in this array
is now going to be based on names.
So in Bash I do this by having to declare the array,
using a specific type.
And that type is dash capital A.
Let's go ahead and declare a named array
or associative array inside of Bash.
To do this I'm going to use declare dash capital A
and then the array name,
in my case,
I'm going to call it phone book.
Now when I want to get information into that
I'm simply going to go ahead and say phone book bracket,
and the key I want to use,
let's say name.
Then I'm going to equal that to the name I want to store
such as Jason.
Then if I want to put additional information in there,
I can simply do something like phone book,
bracket number equals,
then the number 111 dash 1111.
Now if I want to get information back out of this array,
I can do it by doing dollar sign curly brace phone book
bracket name bracket curly brace,
and this will then give me the information back out,
such as the name which was Jason.
If I wanted to get the phone number back out,
I would simply use dollar sign curly bracket hone book,
bracket number bracket curly brace,
and then number will come back out as 111 dash 1111,
which was the phone number I stored in there.
This essentially is storing these key value pairs
inside of the array for us.
And this is a way that we can use these associative arrays.
Next let's talk about doing comparisons inside of Bash.
Flow controls are going to be based on doing some kind of a
comparison like is something equal or not equal
or greater than,
or greater than or equal to,
less than,
less than or equal to,
and things like that.
Now when we're doing arithmetic comparisons inside of Bash,
we're normally going to use text to signify that
instead of actually using the symbols.
For example,
if I wanted to compare two variables known as A and B,
I can do that by doing if bracket quote dollar sign a
quote dash eq quote dollar sign b quote bracket.
This says is a equal to b.
If it is,
it returns a value of one.
If it's not,
it returns a value of zero.
Now if I wanted to check if two things are not equal,
I can do that by using dash ne.
So it would be if dollar sign a dash ne dollar sign b,
that's going to check is a not equal to b.
So if a and b are equal,
I will get a zero.
If they're not equal,
I'll get a one because this
is basically reverse bullion logic.
If I want to do something as greater than something else
I'm going to use dash gt.
If I want to say is something greater
than or equal to something else I'm going to use dash ge.
If I want less than I'm going to use dash lt.
And if I want to use less than or equal to,
I'm going to use dash le.
Now in addition to this,
in newer versions of Bash,
you can use the arithmetic symbols
such as the less than sign,
the less than equal to,
the greater than,
and the greater than equal to sign.
But when you do that,
you need to use double parentheses
around the thing you're testing.
So instead of saying,
if bracket dollar sign a dash le dollar sign b bracket,
we would then use parenthesis parenthesis dollar sign a
less than dollar sign b parenthesis parenthesis.
This double parenthesis replaces the word if
and those brackets inside of the comparisons.
In addition to being able to compare mathematical things,
we also sometimes need to do comparisons with strings.
For example,
are two strings equal?
Well we can do that if we use the if command.
So again,
we're going to use if bracket dollar sign a
equals dollar sign b bracket.
Notice when we were doing mathematical comparisons,
we were using the dash eq in this format,
but now because we're using the equal sign,
we're doing this as a text string comparison.
This would test something like the word Jason
against the word Jason.
If one had a capital letter and one had a lower case letter,
those two words are not going to be equal,
and we're going to get a zero return by this function.
Now in addition to using a single equal,
you can also use a double equal sign,
and these are equivalent
and it doesn't really make a difference
when you're doing a string comparison.
Now the next thing we have is the not equal to.
If you want to compare two strings
and see if they are not equal to each other,
you can do this by comparing them using
if bracket dollar sign a bang equals dollar sign b bracket,
essentially we are going to use an exclamation equal sign
to mean not equal to.
Next we have the less than and greater than.
And again,
we can compare string text using less than or greater than.
Now you may wonder how do you compare two things
that are words?
Well we do it based on ASCII order.
For example,
if I compare the word Jason to the word Dion,
Jason is greater than Dion because J comes after D
in the ASCII alphabet.
To use these you're going to do if bracket dollar sign a
slash less than dollar sign b bracket.
Notice I had to have that slash there.
This is an escape character
because we are inside a single bracket.
If I didn't want to use that escape character,
I can type it out as if bracket bracket dollar sign a
less than dollar sign b bracket bracket instead,
and that way I don't need to use the escape character.
This same format is also used for the greater than sign,
where you're going to use double brackets
with just the greater than sign or single brackets
and the slash and the greater than sign
when comparing two ASCII strings against each other.
Next we have our logical comparisons inside of Bash.
The most basic form of this is an if then statement.
Inside of Bash you're going to do if,
the condition you're testing,
then the command you want to run,
and then you're going to close it out using fi.
This takes the place of using any kind of brackets
or parentheses inside of these function calls.
Now if you want to do something a little bit more complex,
you can use something like if then elif then else fi.
Now what this means is I'm going to test a first condition,
then do some code if it's true.
If it's not true,
I'm going to go into a second else condition.
I'm going to test that condition.
If it's true,
I'm going to do some code.
If it's not,
then I'm going to do the third option.
Now the next thing we have is our loops
and we call these our four our do while
and our wild commands.
We went through the different flow control options
in an earlier lesson.
Now in Bash we actually term them a little bit different.
When we talk about the four loop,
we are really talking about a four do done,
and that's the statement syntax we're going to use.
When you use a four do done,
you're going to perform a set of commands
for each item in a given list.
This is going to take the form of four variable in list
do some commands done.
So for example,
for value in curly brackets one dot dot five
end curly brackets do echo dollar sign value
done echo all done.
This script is basically going to do counting.
It's going to start out and set the value to one,
and then it's going to echo value of one to the screen.
Next it's going to set it to two and echo that to the screen.
Then three,
then four,
then five.
Once all five are done,
it's then going to go to the final echo command
and say all done.
So what you'll see on the screen is 12345all done.
The second one we have is what's known as a do while.
Now a do while inside a Bash is instead
called a wild do done.
This performs a set of commands while a test is true.
So it's going to take the form
of while some test do some commands done.
Consider the following sample script,
counter equals one while compare the counter variable
less than 10 do echo counter then counter plus plus
done echo all done.
So what is this doing?
Well we're going to start out by setting the counter to one.
Then as long as the variable of counter is less than 10,
we are going to go through this loop.
In this case,
we just set the variable to one,
so one is less than 10.
So we are going to do the echo
or printing something to the screen,
which is the counter.
So we print one to the screen.
Then counter plus plus means take the value of counter,
add one and save it back to the counter variable.
So now my counter is two.
Is two less than 10?
Yes it is.
So we'll print that to the screen.
We'll increment the three.
We'll continue doing that all the way through as we go
through nine it's less than 10 we'll print nine to the
screen we'll increment and now we're at 10.
When we check that,
10 is not less than 10,
so we'll stop.
So what you'll see is a counter.
That's going to print 123456789All done to your screen.
The final one we have in Bash
is going to be the wild command.
And in Bash we actually call this until do done.
This performs a set of commands until a test returns true.
So we'll do until some test do these commands done.
For example,
counter equals one until counter is greater than five
do echo counter counter plus plus done echo all done.
This says we're going to set our counter at one
until the counter is greater than five,
we're going to do everything in the loop.
Since our counter is one,
we're going to echo that counter to the screen.
Then we're going to increment the counter and make it two.
Is two greater than five?
No.
So we're going to keep doing that function.
Echo two to the screen,
make it three is three greater than five?
No.
Print that to the screen.
Is four greater than five?
No.
Print that to the screen.
Is five greater than five?
No.
So print that to the screen.
And then is six now greater than five?
Yes.
We can stop doing that function.
This is the way flow control works inside a Bash
when you're using these different types of controls.
Next we need to talk about some basic string operations.
String operations are the commands that are used
to manipulate data that is of the string format,
things like words.
Now when you're dealing with strings,
you're going to have a variable like test string equals
quote test space string.
That means that variable of test string now contains
the words test space and string.
Now that I have that string stored as test string,
I can display it in multiple ways
by manipulating what is going to be displayed.
If I use the echo command and use dollar sign test string,
it's just going to show me the entire thing,
test space string,
but I can also manipulate that string before displaying it.
Now if I wanted to show a sub string,
I can do that by using positioning inside
of calling that variable.
So let's say I did echo dollar sign curly bracket
test string colon 2 colon 4 end curly bracket.
What does this say?
Well this is saying that I want to start at position two
and I want to count four places from there.
So in my case of using the test string as my variable
with the contents of test space string as my string,
I can now have starting at position two,
which is the S,
I'm going to count four places.
So st space S,
that is what I'm going to show on the screen
when I use this echo command.
There are lots of other ways to do string operations,
and we're just touching the surface here,
but I just wanted to introduce you to the concept
that what you have stored in a variable
can be modified when you display it
or modified and stored back into the variable.
The next thing we need to talk about in terms of Bash
is going to be inputting and outputting data.
You want to take input from the keyboard,
from your user,
and give output back to the monitor
when you want to give them information.
I've already used echo many times in this demonstration,
but echo is the easiest way
to send things back to the monitor.
You simply use echo and the variable
or echo and a quote with whatever string you want to display.
For example,
let's say I wanted to say,
please enter your name on the screen.
So I would do echo quote please enter your name
colon end quote.
That's going to display that string on the screen.
Now if I wanted to read information from you,
I can use the command read
and a variable name such as username.
Now when you enter in your username and hit enter,
it's going to be stored in the variable username.
If I want to display that back to the screen,
I would then type echo quote hello dollar sign username
exclamation point end quote.
And so what this would look like is please enter your name.
I would type in Jason,
hit enter,
and then you would see hello Jason.
That's the idea of input and output to a screen.
Now in addition to inputting and outputting
things to the screen,
you can also read or write data into files.
If you want to do this in Bash,
it's quite simple.
You're going to create a variable name
that's going to act as that file.
For instance,
temp file.
You're going to set that equal to dollar sign parenthesis
less than the file name end parenthesis.
So I might use something like temp file
equals dollar sign parenthesis less than test
dot txt end parenthesis.
By doing this I have just set all the contents
of test dot txt into the temporary file.
Now if I want to show the contents of that file,
I'll simply use echo dollar sign temp file,
and that'll display the contents of that file
that I just put in that variable to my screen.
Now on the other hand,
if I want to send data to a file,
I can use the greater than symbol
instead of the less than symbol.
Less than means input.
Greater than means output.
Now there's two ways to write to a file.
One is to use the single greater than sign.
When you do this,
any input you give is going to go into that file.
If that file already exists,
it's actually going to be overwritten with what you put there.
Now if you want to append things to that file,
you're going to use a double greater than sign.
So for example,
if I said echo quote
this is now going to be added to the end of the file
as of the next line end quote
greater than greater than test dot txt,
that's exactly what's going to happen.
That string I just typed is going to be echoed
instead of going to the screen,
it's going to go into the file at the end of the file
without overriding the current contents.
All right let's go ahead and take a look at a short script
written in Bash and try to understand what it's doing.
Well the first command we have is if dollar sign one
does not equal empty.
What is that telling us?
It's saying that if there is nothing there,
then don't do anything.
But if it's not empty,
then our target file is going to equal dollar sign one,
that variable.
We're going to set our report directory
to var slash log slash nmap.
So we're probably going to be doing something with nmap
if we're setting it to the nmap directory.
Next for target in dollar sign cat target file.
Now there we can see that for the variable target,
we are going to assign each line inside that target file
to the value of target,
and then run through that loop.
Once we run out of lines,
that would be the end of that for loop,
and we will be finished with doing these commands.
Next we're going to echo scanning target,
whatever that target is that we just pulled in
from that target file. So what do we have on the next line?
Well we have slash user slash bin slash nmap dash ON
and then a lot of stuff,
as you can see,
it takes up over two lines.
Now what is this telling us to do?
Well this is saying we're going to run the nmap command
and we're going to use it with these different parameters.
And then we have the done.
Otherwise we're going to echo you did not provide any command
line options usage program parenthesis file echo echo
and then fi to end the if loop.
So what is this short Bash script really doing for us?
Have you figured it out yet?
Well if not pause the video,
look through it,
and then restart it so you can see
if you got the right answer.
All right you're back?
Great.
So what is this script doing?
Well it's going to take in from the command line
we're going to run it by writing the program name,
the script,
and then giving it a file.
Every time there is something in that file,
like an IP address or a domain name,
it's going to run an nmap scan against it.
So all this command is doing is allowing me
to take whatever inputs from a file,
one per line for IP addresses
and run a different scan on them each time.
So in this case,
I'm going to pull the file in,
I'm going to read one at a time from there,
and every time there's something on the line
and it's not empty,
I'm going to run an nmap scan against it.
Now what is this nmap scan doing?
Well this is a slow comprehensive scan.
We're using T4,
which is not too aggressive.
We're using dash a,
which searches for everything
including OS detection and versioning.
We're doing SS for a sin scan.
We're using a dash ON for output files in nmap format,
and each one is going to be saved to its own file
named target dot nmap.
So if I had three IP addresses,
say I had scanme.nmap.org,
Jason ion.com and Diontraining.com,
it's going to pull each of those.
It's going to do a full nmap scan and save a file,
Jasondion.com dot nmap,
Diontraining.com dot nmap,
right? And I can go back and look at those results later.
So I can run this script,
go home,
come back the next day and have all my scans completed,
it's really great.
And so instead of me having to run the scan
three different times on three different IPS,
I can just give it a file
and the script will pull the information out
and run those scans for me.
So what might a question like this look like on the exam?
Well they might give you something like this and then say,
what's the function of it?
And the correct answer would be that this is performing
an nmap scan against targets provided from an input file,
something like that right?
That's the idea here.
And so you don't need to understand every single nuance
of this script,
but you should be able to identify that it's a Bash script
and what the basic function is.
In this case,
it's going to go through a file,
read each time what's in that file,
and run an nmap scan against them.