Looping in BASH

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello, cybrarians and
00:00
welcome back to the Linux+ course here at.
00:00
Cybrary. I'm your instructor Rob Gills.
00:00
In today's lesson, we're going to be
00:00
talking about looping in BASH.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to understand
00:00
the purpose of looping in
00:00
BASH and differentiate between
00:00
all types of loops that we could use in BASH.
00:00
Then during our demo at the end of the lesson,
00:00
we'll see how to use loops in our BASH scripts.
00:00
When we're writing scripts, sometimes we
00:00
need to repeat a command multiple times,
00:00
or maybe we need to iterate over file content,
00:00
and this is when the concept of looping comes in handy.
00:00
Now, in BASH,
00:00
there are two loop types,
00:00
there's the for loop and then there's the while loop.
00:00
What we're going to do is take a look at
00:00
both types of loops in this lesson.
00:00
The for loop iterates through
00:00
every single element and
00:00
that could be in a list or series.
00:00
For example, we can look for every file in
00:00
directory and then do
00:00
something upon every file in directory,
00:00
or we could go through every line in
00:00
a text document and then
00:00
perform some action on each line.
00:00
In general, the syntax that we're going to
00:00
use when we're using a for
00:00
loop is for items in
00:00
that series or element in a list or whatever,
00:00
do commands and then done.
00:00
We could also write this as a BASH shell one-liner.
00:00
We can say for items in series;
00:00
do command; done.
00:00
For example, if we wanted to rewrite
00:00
our script that looks for conf files in etc,
00:00
we can write that as one-liner.
00:00
We can say for file in and
00:00
then les etc grep star dot conf.
00:00
What we're doing is we're listing all of
00:00
the contents of etc,
00:00
but we only want to return
00:00
the contents of etc that end dot conf,
00:00
in other words, the dot conf files.
00:00
What we're doing is we're assigning
00:00
that content to the file,
00:00
if the file is the variable name that we're using,
00:00
and then what we do is
00:00
>> we do echo on that file variables.
00:00
>> We're doing variable expansion and then done.
00:00
The end result is it just displays the name
00:00
of every file that ends dot conf,
00:00
just like our scripted previously,
00:00
but we just do it in one line.
00:00
Now, the while loop continues
00:00
as long as a condition that
00:00
specified at the opening of the loop evaluates as true.
00:00
What that basically means is it usually will do
00:00
a while loop like while
00:00
something is less than something else,
00:00
and then at the end of every
00:00
>> iteration through the loop,
00:00
>> we'll increment that by one.
00:00
Eventually we'll reach the point where
00:00
something is no longer less than somebody else.
00:00
We'll say while condition equals whatever,
00:00
do some commands and then done.
00:00
We can say, while one is less than five, do something,
00:00
and then we'll say, add something
00:00
to one until we get to five,
00:00
and it'll loop through that condition
00:00
every time until we get to five.
00:00
This can also be rewritten as a BASH one-liner.
00:00
We could do while condition and
00:00
then do commands and then done.
00:00
Let's take a look at some loops with some demo time.
00:00
Here we are back in our demo environment in Ubuntu
00:00
and let's take the one kind of loop,
00:00
at least in a new script.
00:00
I've already created a file here called
00:00
loops.sh script file and
00:00
so what we're going to do is open this up.
00:00
Now, we can see as per usual,
00:00
we have our interpreter here,
00:00
we have our shebang and then bin BASH,
00:00
indicating that we're using BASH.
00:00
Then we've written little couple of
00:00
comments here indicating what this does.
00:00
Now, what we're going to do is we're
00:00
going to go ahead and loop through
00:00
a folder and we'll ask
00:00
the user which directory do we want to search in,
00:00
and we'll read that information in as search directory.
00:00
Then what we'll do is we'll take
00:00
that variable search directory and
00:00
we'll change directory into that search directory,
00:00
and now we're going to run this forward.
00:00
What we'll do is we'll say for object in ls sort.
00:00
We're going to list all the contents of
00:00
this search directory that
00:00
>> we're already in and sort it,
00:00
>> and then we're going to hand that back to this object.
00:00
This is the variable that we're going to
00:00
use throughout the rest of the script.
00:00
We'll say for object in
00:00
the sorted list of contents in
00:00
the search directory, do something.
00:00
Now, we're going to use this object everywhere
00:00
else is our variable inside of the script down here.
00:00
Now, we're going to do
00:00
some traditional tests and we'll say
00:00
if this object is a file,
00:00
because remember we have this file test,
00:00
the dash f,
00:00
indicates that something is a file.
00:00
We say if the object is a file,
00:00
then echo object is a normal text file.
00:00
Or we could test,
00:00
we can say else if or lif the file is a directory,
00:00
because remember dash d is the file test for directory,
00:00
so we say else if the file is a directory,
00:00
then we say echo object is a directory.
00:00
If we don't know what it is,
00:00
we can say Rob isn't sure what object it is.
00:00
We close this if statement by
00:00
doing the f remember is just if spelled backwards,
00:00
and then we close the for loop by saying done.
00:00
This an example of where we're doing
00:00
looping and we're using
00:00
>> conditionals inside of our loop.
00:00
>> I want it to do that here so we can see
00:00
how both of those could be used with
00:00
each other in order to write
00:00
a really cool script that does something pretty basic.
00:00
Now, let's go ahead and just get out of this file.
00:00
I'll hit "Escape" 'Colon" "W" "Q"
00:00
>> and now we can call it.
00:00
>> We'll call loops.sh. Now,
00:00
we can say, well, what directory you want to search in?
00:00
Well, it's certainly my home directory.
00:00
I know I have permissions to it on
00:00
off becomes pseudo or anything.
00:00
We'll just say searching home
00:00
rob and now what we'll see is
00:00
it'll look through all of the files
00:00
and indicate what they are.
00:00
It'll say Alpha sort is a normal text file,
00:00
Beta is a directory, so on and so forth,
00:00
and we could verify that this is true.
00:00
We could just do an ls on home rob.
00:00
Now, we can see Alpha sort well that is
00:00
a text file apparently
00:00
been Z is a directory sent file is
00:00
a text file and so on and so
00:00
forth all the way down the line.
00:00
With that, we've reached the end of this lesson and in
00:00
this lesson we covered the purpose of looping in BASH.
00:00
We talked about the types
00:00
of loops that are used in BASH,
00:00
and then we covered using loops in
00:00
BASH scripts during our demo at the end of the lesson.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next