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 MacOs.
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, Perl, 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 #!/bin/bash.
This means you're going to see that hashtag
which normally signifies a comment line
and then the bang or exclamation mark.
Then /bin/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 #!/bin/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, '#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 #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 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 $CustomerName.
and the $CustomerName 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 to be treated as a number,
I can declare it by saying declare -i
then phone number equals,
and whatever value I want, like 1111111.
This is now my seven digit phone number
and it'll 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 -r Pi=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 parentheses and separate by commas.
So tempArray=(value1, value2, value3).
Now I have an array with three different items in it
listed as value1, 2 and 3.
If you want to get information stored in an array back out
inside a 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 and 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 works almost like a table inside of a database
and any data stored in this array
is now going to be based on names.
When we talked earlier about dictionaries,
this is kind of like what we're talking about
with a named array.
So in Bash I do this by having to declare the array
using a specific type and that type is -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 -A
and then the array name.
In my case, I'm going to call it phone book.
Now what 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-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 would 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, phone book,
bracket, number, bracket, curly brace,
and the number will come back out as 111-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.
As we talked about before,
we're going to do a lot of different flow controls
and a lot of those flows 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 a 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 ["$a" -eq "$b"]
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 -ne.
So it would be if ["$a" -ne "$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 Boolean logic.
If I want to do something is greater than something else,
I'm going to use -gt.
If I want to say is something greater than
or equal to something else, I'm going to use -ge.
If I want less than I'm going to use -lt.
And if I want to use less than or equal to,
I'm going to use -le.
Now in addition to this in newer versions of Dash,
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 ["$a" -le "$b"]
we would then use (("$a" < "$b")).
This double parentheses 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 ["$a"="$b"].
Notice when we were doing mathematical comparisons,
we were using the -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 lowercase 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 could 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 ["$a" != "$b"].
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 ASQII 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 ASQII alphabet.
To use these, you're going to do
if ["$a" \< "$b"].
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 [["$a" < "$b"]]
instead.
In 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 ASQII strings against each other.
Next we have our logical comparisons inside a bash.
The most basic form of this is an if then statement.
Inside a 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 a 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, else-if 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 lubes
and we called these our for, our do while
and our while 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 for loop,
we are really talking about a For Do Done,
and that's the statement syntax we're going to use.
When you use a For 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 for variable in list do,
some commands, done.
So for example, for value in {1..5}
do echo $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 the do while inside a bash
is instead called a While 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=1,
while compare the counter variable less than 10
do, echo, counter, then counter++, 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++ 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's.
So we'll print that to the screen.
We'll increment to three.
We'll continue doing that all the way through
as we go through nine is 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 while 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++, 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
testString="Test String".
That means that variable of test string
now contains the words test 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 $testString,
it's just going to show me the entire thing, Test 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 ${testString:2:4},
what is this saying?
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 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 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 "Please enter your name:"
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 "Hello $Username!"
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, TempFile.
You're going to set that
=$(<filename).
So I might use something like TempFile=$(<test.txt).
By doing this, I have just set all the contents of test.txt
into the temporary file.
Now, if I want to show the contents of that file,
I'll simply use echo $TempFile
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
"This is now going to be added to the end of the file
as the next line >> test.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 overwritting the correct contents.
I know that was a lot of information
as we started covering the basics of Bash.
Now the good news is
as we go into our other programming languages,
we're not going to have to explain everything as much
because we just covered it.
Instead, I'm going to be explaining
what the exact syntax is in those languages,
but I'm going to be leaving the explanation
of how each of these things work inside of this lesson only
because we already covered all those commands inside a bash
and so now we only need to learn the differences
for the other languages that we're going to be looking at.