Overview
Join Shawn Powers as he discusses the benefits of Linux scripting and using BASH.
Learn the ins-and-outs of various editors and the benefits of using BASH shell scripts, variables, conditionals, and loops.
Scripting: Editors — Entry Level
Linux includes a wide variety of text editors, any of which can be used for creating BASH scripts. In this Nugget, we look at a few of the more common editors.
Knowledge Check
Which of the following best describes a BASH script?
Knowledge Check
Syntax highlighting only works with GUI editors. True or false?
Knowledge Check
Which of the following editors are best to use for BASH scripting? (Choose five)
Scripting: Using BASH Shell Scripts — Entry Level
Linux scripting is an invaluable time saver for anyone who types commands on the command line. In this Nugget, we learn how to create a shell script, how to make sure the BASH interpreter executes it, and how to make it an executable application itself.
Knowledge Check
What does "BASH" stand for?
Knowledge Check
BASH scripts must end with the .sh extension for the command interpreter to know it's a script. True or false?
Knowledge Check
The combination of "#!" is referred to as which of the following?
Knowledge Check
If you make a script _____, it will run like any other program, without needing to type "bash scriptname" to make it run.
This interactive assessment is available in the full learning experience.
Scripting: BASH Variables — Entry Level
Variables are constructs that can hold values and then can be used as references to those values. In this Nugget, we learn to use variables, along with how to assign them.
Knowledge Check
Which of the following is true when assigning a variable in BASH?
Knowledge Check
In order to prompt the user for information to assign to a variable, we use the _____ directive inside our BASH script.
This interactive assessment is available in the full learning experience.
Knowledge Check
In order to echo the first argument provided to a script on the command line, the directive inside the BASH script should be which of the following?
Knowledge Check
To assign a value to a variable inside a BASH script, you must use which symbol?
Scripting: BASH Conditionals — Intermediate
Scripting becomes even more powerful when logical conditionals are added. In this Nugget, we learn to use if/then statements along with conditional tests in order to make smarter and more functional scripts.
Please Note:
The script shown in this video contains a small typo:
-w= Writable — checks whether a file can be written to-r= Readable — checks whether a file can be read
Please use the definitions above when following along with the video.
Knowledge Check
Every IF/THEN statement requires an ELSE, otherwise the script will stop with an error if the conditional is false. True or false?
Knowledge Check
Which of the following ends an IF/THEN section in a script?
Knowledge Check
Indentation determines which commands are executed in an IF/THEN statement. True or false?
Knowledge Check
When performing File Tests, which of the following checks to see if a file exists? (Choose two)
Scripting: Nested Conditionals and Case Statements — Intermediate
Sometimes the if/then/else logic isn't quite enough to solve your programming needs. In this Nugget, we learn how to specify multiple conditions, nest logic statements, and provide responses for more than just true/false conditions.
Knowledge Check
Match the following symbols with their definition.
This interactive assessment is available in the full learning experience.
Knowledge Check
When using ELIF, it's not necessary to close each ELIF with an FI. True or false?
Knowledge Check
If no matches were found, which character acts as a "catch all" at the end of a case statement?
Knowledge Check
Which of the following is the best type of conditional construct?
Scripting: BASH While and Until Loops — Intermediate
Loops are perfect for doing the same thing more than one time. With While and Until loops, you can use a conditional test to determine if a loop should run again.
Knowledge Check
To show the beginning and end of a while loop, which of the following statements should be used?
Knowledge Check
Which construct is used to compare two string variables to see if they match?
Knowledge Check
How do you stop an endless loop?
Scripting: BASH FOR Loops — Intermediate
Using FOR loops, it's possible to repeat the same process on a set of data. It's a little more confusing than While and Until loops, but once mastered, it's an invaluable tool for the system administrator.
Knowledge Check
To create a loop that prints the numbers 1-15 with each number on its own line, which order must the following BASH lines be in?
This interactive assessment is available in the full learning experience.
Knowledge Check
Which of the following should be used to print a sequence of numbers, incremented by three, within a range of 1 - 20?
Knowledge Check
To use the output of the ls command as the data set in a FOR loop, which of the following can be used?
Knowledge Check
FOR loops rely on conditional tests when creating a data set. True or false?
Conclusion
I hope this has been informative for you and I would like to thank you for consuming.
View Transcript
Scripting: Editors — Entry Level
0:00No, it may seem like a silly thing to talk about,
0:02but when we're talking about bash scripting
0:04it's really important that we figure out
0:06what text editor we want to use, because bash scripts
0:09are text only.
0:11So you can either use a GUI tool or a command line interface
0:14tool, that's what CLI stands for.
0:16And I really want to talk about what
0:17not to do when it comes to writing
0:20bash scripts with your editor.
0:21So the first thing we need to figure out
0:23is what is the best way to go about editing a text file.
0:27Now you've probably heard the saying,
0:29there are many ways to skin a cat.
0:31Well I think that's an absolutely horrible saying,
0:34we will not talk about skinning cats.
0:36Rather, I want to go right to our command line
0:38so we can look at the various types of editors
0:41we have available to create these bash
0:43scripts that we're going to look at.
0:44Now since we're using a GUI environment or a graphical user
0:48interface on top of Linux, one of the things you're able to do
0:51is use a GUI or a graphical user interface text editor.
0:54Now this one happens to have Pluma text editor installed.
0:58If we open this up, you'll see it's a GUI tool.
1:00And we could open a file, like you'll notice on the desktop
1:03I have this SampleScript that I put on here.
1:06And it'll open, and you can edit it, and then save it.
1:09And it'll work just like every other program you would expect.
1:12Now it does have a few cool features,
1:15for example, syntax highlighting.
1:16Now this is just plain text, but if you look
1:19it actually highlights things based on the code.
1:22So this is bash code, and it knows how to highlight things
1:25differently.
1:26So you know this is green.
1:27This is red.
1:27This is purple.
1:28That's really useful, because if you accidentally do
1:31a typo, like if you misspell a command like while,
1:34it's not going to highlight it, because it's not
1:36going to recognize it.
1:37So it's going to look weird.
1:39It's just a really nice way to keep things organized,
1:42and be able to identify things quickly.
1:44If you forget this close quotation,
1:46for example, it's going to highlight things differently
1:49and that's not what we would expect.
1:51So if we close that it's going to make it look right.
1:53And it's going to be highlighted just
1:55like the rest of the things.
1:56So syntax highlighting is powerful.
1:58You want to use syntax highlighting
2:00if at all possible.
2:01Regardless of whether you're using a GUI or a text
2:04based editor.
2:05Now again this is perfectly acceptable,
2:07if you want to do this.
2:08I'm not going to save any changes here.
2:10You can use that GUI tool if you want.
2:13Now the one thing you don't want to do,
2:14though, is to use a GUI editor that happens
2:17to be like a word processor.
2:19So for example, LibreOffice Writer is here,
2:22you do not want to edit text files with LibreOffice.
2:26Now is it possible?
2:28Sure, we could say file, open, and go to the desktop,
2:32and find our SampleScript.
2:33And it's going to be here, but a couple problems
2:36with this, one you'll notice there's no syntax highlighting.
2:39It just sees this as a text file,
2:41and it wants you to edit things.
2:42Using this we could make it bold,
2:45and things like that, which is not text only.
2:48We only want it to be text, and things like this
2:50just are not compatible with bash scripts.
2:53So do not use something like LibreOffice or Microsoft Office
2:57when creating bash scripts, it's just not ideal.
3:00So I'm gonna say, don't save.
3:02Now if you're using Linux chances are at some point
3:06you're going to find yourself in situation where you only
3:08have access to the terminal.
3:10For example Linux servers are often
3:12called headless, meaning they don't have this graphical user
3:15interface.
3:15So if you're stuck in a position like that where you only
3:18have text, it's really important to understand text editors.
3:22So let's go to the desktop and inside here we'll
3:25see that SampleScript.
3:27Now, if you type nano, nano is the name
3:30of a very powerful text editor.
3:32It's the one I recommend that you
3:33use as we edit files inside a command line window in Linux,
3:39in general.
3:39Nano is just awesome.
3:41It's really easy to use.
3:42You just type nano space the name of the file
3:44you want to edit, press Enter.
3:46You'll notice it does have syntax highlighting which
3:48is awesome.
3:49We would hope that it would have syntax highlighting.
3:52And you can edit files, just simply--
3:55like you would arrow keys and make changes.
3:59And then when you make a change and you want to save it,
4:01there's a little cheat sheet right here at the bottom.
4:03So Control X, for example is how you exit.
4:07So we do that.
4:08It says, do you want to save the modified buffer.
4:10That's basically saying, do you want to save your changes.
4:12You can either do yes, no, or control-c to cancel.
4:15So I'm going to press the Y key.
4:17And then it says what file name do you want to save it as.
4:19Well it's the same file name by default,
4:22and we're going to leave that and press Enter.
4:24It'll save over top of what's there.
4:26But we could actually change the name.
4:27We could say SampleScript 2, and then it
4:29would act like save as, if you're
4:32going to save it as something new we would have two things.
4:35I actually want to save it right over top,
4:37so I'm going to leave the default where it says file name
4:40to write SampleScript, press Enter,
4:42and now this script has been changed.
4:45It's really cool, since we have a GUI environment,
4:47you could do something like this,
4:49if you double click on the script, it's going to say,
4:51all right so this is executable, do you
4:53want to run it in the terminal.
4:55And if we do that, it's going to open a new terminal
4:57and execute the bash script.
4:59Now this is simple little bash script
5:00I made to show you the syntax highlighting and stuff,
5:03however, it does give us instructions.
5:05If you want to stop bashing, press control-c.
5:07So I'll press Control c, and it'll close that window.
5:10So the GUI does give us a couple of nice things,
5:13you can double click on it and say Display,
5:16and it conveniently will then open it in Pluma.
5:19So you can make changes right inside this GUI environment.
5:23So by all means, you can use the GUI tools here.
5:26But knowing how to use it on the command line is important.
5:29I'm going to do everything inside the command line.
5:31And I'm actually going to use an editor called vi.
5:35Now I don't recommend that you use vi,
5:37only because while it does support syntax highlighting,
5:41it's a really complicated editor to get the hang of.
5:43The only reason I use it is because I've been using it
5:46for over 20 years.
5:48Things like nano didn't used to exist.
5:50And so I had to learn to use vi and now
5:52my fingers and brain kind of just
5:53go on autopilot, because in order
5:55to edit you have to do really bizarre things.
5:57Like go to this.
5:59And then you have to go into Insert mode
6:02in order to make changes.
6:04And then you have to press Escape
6:05to get out of Insert mode.
6:06If you want to save it, you have to type colon
6:09wq, which is not intuitive, and there's no cheat sheet.
6:14But this is the editor that I'm going to use
6:17as I show you how to do things.
6:18But again you can follow along and do everything
6:21I'm doing in vi, but use nano.
6:23And it's going to be a lot easier,
6:24and it's really what I recommend you use.
6:27I could use nano but again vi just comes naturally.
6:30If you want to play with vi, by all means, you can do that.
6:33It's going to be installed on every Linux and Unix box
6:35you log into.
6:36Vi is going to be an option.
6:38However nano is going to be there almost as often, so I
6:42really recommend using nano.
6:44Regardless of what editor you use
6:46it's important to remember that it has to be text only.
6:49So do not use a word processor as we're
6:51editing these bash scripts.
6:54So while I don't agree that multiple ways to skin a cat
6:57is a good idea, I do agree that it
6:59is important to understand there are multiple ways that we
7:02can edit text files.
7:04And not one is correct, it's just
7:06that it's going to be personal preference.
7:08Although I do recommend that you do your best
7:10to learn how to use a command line interface text
7:13editor, because with those headless servers
7:16you're not going to have a GUI option.
7:17So if you rely on a graphical user interface
7:20you're going to be struggling when it comes to servers
7:22that don't have a GUI.
7:24So with this lab I urge you to play around
7:27with the different text editors that are here,
7:28get comfortable with one, and whatever one you pick just
7:32use that as we go along and learn how to do bash
7:34scripting in other Nuggets.
7:36Again text is text, it doesn't matter
7:38if you do the exact same thing I do
7:40it's just important that the end result is the same.
7:42I hope this has been informative for you,
7:44and I'd like to thank you for viewing.
Scripting: Using BASH Shell Scripts — Entry Level
0:00You can kind of tell how long somebody has been using Linux
0:02by how much they rely on scripting on their day
0:05to day system administration.
0:07Scripting is awesome.
0:08It's a great way to save time--
0:10if you want to call it being lazy you can-- but basically,
0:13all of those shell commands that you would type in
0:15on the command line one by one, you
0:17can make a script or a file that contains a bunch of them,
0:21and then the shell will allow you
0:23to execute them all at once.
0:24Now usually it's the BASH shell that we're using.
0:27Any modern Linux distribution is going
0:29to default to using the BASH shell, which
0:31is Born Again Shell is what that stands for.
0:34But that's just the interface that you
0:35have on the command line.
0:37So this is a very simple thing that we can set up.
0:40I want to show you the structure of how
0:41to set up a shell script or a BASH script.
0:44And I want to talk about calling it versus executing it.
0:47And then we're going to learn what a shebang is
0:49and why you want to use a shebang in your shell scripts.
0:52So let's go to the computer and start by making
0:54some simple shell scripts.
0:55Now, I'm here on an Ubuntu computer.
0:57It doesn't matter what Linux you have,
0:59because they're all going to have the Bash script
1:00and we're going to be able to do some things.
1:02I could type ls and it's going to show
1:04us the files in our directory.
1:06I could type ls minus l to get a long listing
1:09so we can see all the files.
1:10We could say something like echo hello,
1:14and it's going to echo hello on the command line.
1:16These are all things we can do on the command line.
1:18I'll say expr 1 plus 1, it's going
1:23to give us the answer of 2.
1:24So these are all things we can do on the command line.
1:26But wouldn't it be nice if we could do this in a script.
1:29And let's actually put that inside a file,
1:32and we'll execute it as a shell script.
1:34Now, I am going to use vi, that's the editor I use.
1:37I just prefer vi, that's what I use.
1:39You can use anything you want-- nano or a graphical editor.
1:42There is a couple.
1:43If you go into this menu, there's
1:44going to be some options.
1:46If you want to use the Pluma Text editor you can do that.
1:49I'm just going to use vi because that's
1:50what I'm comfortable with.
1:51And generally, shell scripts are either
1:54named something like stuff.sh or just any text name.
1:58We don't have to put the extension to make it work.
2:00Some people put the file extension sh
2:03to stand for a shell script.
2:04I generally don't.
2:05I generally just call it like a text file with no extension.
2:08So we're going to call this one stuff,
2:10and here we have an empty file.
2:12So the first thing I want to do is start typing.
2:14I'm going to put a couple of spaces up there.
2:15Spaces don't really matter-- blank lines.
2:17I'm going to type ls, and then I'm
2:20going to want it to do ls minus l, and what is the other one
2:23we did?
2:24We did echo 'Hello Shawn'.
2:27And we'll do and expr 2 plus 2.
2:31All right.
2:31Notice in here, I'm just typing them
2:33just like I would on the command line.
2:35There's no syntax like that doesn't end in a semi-colon--
2:38there's no weird syntax like some programming languages
2:41have.
2:41We just put it in here like we want
2:43it to execute on the screen.
2:45Now, once we get into loops and stuff sure,
2:47there are some structures that we're going to learn.
2:49But as far as the syntax we need to make it function,
2:51we just type it in there like we want it to be in there.
2:54So let's save this file.
2:55And if we type ls now we're going
2:57to see that file 'stuff' is in there.
2:59If we type cat stuff we're going to see it's in there.
3:01We just saved it so it's exactly the file that we just created.
3:04If you want to execute it, what you can type is BASH stuff.
3:09And what it's going to do, it's going to call the BASH shell
3:12and it's going to tell it to execute
3:14the stuff inside the file that we specified.
3:17Type Enter.
3:18And it did all of those things.
3:20Let's see if it really did do all those things.
3:21So I said BASH stuff.
3:22It did an ls.
3:24And then it did an ls minus l.
3:27And then it said 'Hello Shawn'.
3:29And then it did that expr 2 plus 2.
3:32OK.
3:32So it did all of the things that we wanted it
3:34to do one right after another.
3:36Which is awesome.
3:37That's exactly what we would want.
3:39Now, if you don't want to have to type BASH and then
3:42the name of the file, we can actually
3:43make this an executable file.
3:45So we'll say shmod plus x stuff.
3:50Now if we do ls minus l, we're going
3:51to see that stuff has now turned green--
3:53which it may not be green on your distribution,
3:55but if you're using this lab it's going to be green--
3:57and see that it now has the executable flag, which
4:00means this file is executable.
4:02So we could say dot forward slash stuff,
4:06and it's going to execute right on the command line,
4:08because now it thinks it's an application
4:10or an executable file, and it just executes it.
4:14So this is going to work fine.
4:15Now, there is a problem.
4:17If we were to take this program and we
4:20were to take it to another computer that
4:21maybe isn't using BASH as the shell, well then it might fail.
4:26And that's where shebang comes into play.
4:29So let's edit this file again.
4:30It's executable, but we can still edit it.
4:32It's still just a text file.
4:33And a shebang is the very top line of a shell script,
4:37and it's a very specific format.
4:39So I'm going to type insert here.
4:41The pound sign-- or hashtag I guess is the more proper way
4:44to say it nowadays--
4:45exclamation, and then what shell you want it to use.
4:50So for us it's been BASH.
4:52So this combination of characters right here,
4:55the hashtag and the exclamation, is called shebang.
4:59So shebang, and then the full path
5:02of the executable that we want to use
5:04as the shell interpreter.
5:06So this line is telling the computer
5:08what interpreter do you want to use if we
5:11call this as an executable.
5:12So if this doesn't exist--
5:14notice it didn't exist and it just
5:16worked for us-- what it does is it
5:17uses the current running shell if you don't specify what
5:22shell it's supposed to use.
5:23So for us, since we're currently running BASH,
5:25it worked just fine.
5:27However, to be more proper, you always
5:28want to start your shell script with this shebang and then
5:32the path to BASH, which has almost always been BASH.
5:36And what that says is now the computer
5:38is going to know you want to execute
5:39all the stuff in here with the BASH interpreter or the BASH
5:43shell.
5:44So I'm going to save this, and now of course we
5:47can still type stuff and it's going
5:49to do all those things for us.
5:50It did just like we did before.
5:52But the cool thing about this is--
5:53and this is what I really like about making shell scripts
5:56executable--
5:57is we could say sudo move stuff to user local bin.
6:05It's going to ask for a password, which is pa$$w0rd,
6:10and so now if we type ls it's not in there anymore,
6:13because it went into user local bin.
6:16We can see, sure enough, stuff is in there now.
6:18And since this is in our path, anywhere we are in the system
6:22if we type stuff, it's going to do all those things.
6:25So if we make a shortcut for something that we do often,
6:30we can make it a script, put it in user local bin,
6:33and we're going to be able to execute it just by typing it.
6:36Notice, I don't have to type like dot
6:37and like the full path, and I don't
6:39have to type user local bin, I can just
6:41say the name of it, stuff, and it's
6:43going to execute that shell script that we just created.
6:46Now this is a very, very simple shell script, obviously.
6:49All it does is like ls and a couple of echo commands.
6:52But you can see the beauty of being
6:54able to do a bunch of things in a single command
6:57just like that.
6:57So this is like our building block.
6:59And now you understand how shell scripts work.
7:01What I encourage you to do is make
7:03some shell scripts of your own.
7:04See if you can get them to have the results that you expect.
7:08Make a stuff script of your own and have it do other things.
7:11See if you can cd inside of a shell script.
7:14Can you say cd user local bin.
7:17And if you do that inside of a shell script and you type ls,
7:20what's it going to show you?
7:21The stuff in user local bin or the stuff
7:23in your current directory?
7:24Those are the things I want you to experiment
7:26and play with so that you get a feel for how they work.
7:28But don't forget, you want to put that shebang in there.
7:31Not because it won't work without it on our system,
7:34but because that is a best practice so that it always
7:37calls bin BASH instead of whatever shell
7:39we might be running.
7:41Because sometimes when we start doing things
7:43like loops and conditionals, they'll only work in BASH.
7:46They won't work in some other shell like tcsh or sh
7:49or anything like that.
7:50So you always want to put that shebang bin BASH in there.
7:54Truly, shell scripts are awesome.
7:56And they're really fun.
7:57And making them into executables is--
7:59for some reason it's an awesome way
8:02to make programs that are really just BASH scripts,
8:05but they execute just like programs on your system.
8:08They're awesome.
8:09This is just the tip of the iceberg.
8:11Now that you know how to create BASH scripts,
8:14you should be making them all the time.
8:16Experiment.
8:16See what works.
8:17See what doesn't work.
8:18And in the other Nuggets, we're going
8:19to get more and more complicated to make those BASH scripts even
8:23more useful.
8:24I hope this has been informative for you
8:25and I'd like to thank you for viewing.
Scripting: BASH Variables — Entry Level
0:00Now, batch scripts are incredibly convenient when
0:02it comes to time saving, for putting
0:04a bunch of different commands into a single statement.
0:07However, where they really get powerful
0:08is when you add the concept of variables.
0:10Now, I'm going to try to cover a whole bunch of things regarding
0:13variables, but we're going to do it on the command line.
0:15I actually want to show you variable stuff in action,
0:18but this is just a list of what we're going to cover.
0:21I realized when I wrote it down that back ticks sounds
0:23a little scary, but I assure you,
0:25I'm not talking about bloodsucking critters
0:27on your back.
0:28This is just how we can do a command inside of a script.
0:30So, let's go right to the command line
0:32so I can show you how to use variables inside your scripts.
0:36Now back on our command line here,
0:37I don't have anything in there.
0:38Let's create a script.
0:39So v i.
0:41Actually, let's do touch script, chmod plus x script.
0:47That way it's going to be executable.
0:49So let's edit this file.
0:50First, let's do our shebang then bash on the top.
0:53So we know that it's a bash script,
0:55and then I'm actually going to assign a variable.
0:58Now, a variable is just like you learned in high school algebra.
1:01It stores a value.
1:03So let's say test--
1:05we'll just put that as the name of our variable,
1:07equals-- now this is important, there
1:08can't be a space when you're assigning a variable in bash.
1:11The equal has to be right next to the variable name.
1:15So, test equals-- we'll put in quotes, "some string"
1:21and then down here we'll say echo,
1:24and then how we refer to it is putting a dollar sign in front
1:27of it.
1:28All right, so what this is going to do
1:29is it's going to assign this string to the test
1:32variable and then down here, echo test,
1:34it's going to just echo it to the screen.
1:36Let's actually see how that works.
1:38You can say script and boom.
1:40It just echoed some string.
1:42If we edit it again--
1:44of course now we get really nice syntax
1:45highlighting because it recognized
1:47using the shebang line.
1:48It knows that this is a bash script.
1:50So now it's going to do coloring, and syntax
1:52highlighting, and stuff for us, which makes it easier to read.
1:55Anyway, so we've assigned test.
1:57Now what if we wanted to read something
1:59from the command line?
2:00We don't know what we want it to echo, all right.
2:02So let's say instead of assigning this,
2:05we're going to read from the command line.
2:09And we tell it, what variable do we want it to read?
2:11Well, we'll say test again, that's fine.
2:13So we want you to read the test variable,
2:16but we need to give our command line some sort of hint
2:20as to what it's waiting for.
2:22We'll see what this does when we do it, but echo,
2:25give me some text.
2:28All right, so we say, give me some text
2:30and then it'll wait for us to input text and we press Enter
2:33and it'll assign the text that we enter to this variable.
2:37Let's look at this really quick.
2:39So, script-- it says give me some text.
2:42This is my text, press Enter, and it repeated it,
2:46because remember, the script just
2:48echoed out whatever variable we assigned.
2:50So what we've done is we've allowed some interaction.
2:53So the script will do different things now,
2:55depending on what we enter.
2:56See, it's going to respond whatever text we enter,
2:58it's going to give us.
2:59Now there's another really cool way
3:01to give a script information, so we don't have
3:04to make it part of the code.
3:06Instead of this, we can use a special variable,
3:10and that special variable is going to be dollar sign 1.
3:13Now we can do dollar sign 1, dollar sign 2, dollar sign 3,
3:16and what that ends up being is--
3:19I'll show you, an argument at the end.
3:21So if we do script and we press Enter,
3:24nothing's going to be printed because there are no arguments.
3:27But if we say script, and this is dollar sign 1,
3:34we press Enter, what it does--
3:36this first argument after the script
3:40becomes dollar sign 1 inside the script.
3:43So it echoed dollar sign 1, and we can do the same thing
3:45with multiple ones.
3:47So we could say echo dollar sign 1, dollar sign 2.
3:52Now if we say, script thingone, thingtwo,
3:57it's going to repeat that.
3:59What it did is it assigned dollar sign 1 to this,
4:02and dollar sign 2 to that, and it
4:03used those inside the script.
4:05What if we wanted to do something, not just
4:08with strings?
4:09Now bash often treats things as strings,
4:12but what if we wanted to say "give me two numbers."
4:18Read NUM 1, read NUM 2.
4:22Then we can do something like, expr dollar sign NUM 1,
4:27plus dollar sign NUM 2.
4:30And what this should do is add those numbers
4:32and print them to the screen.
4:33Let's see if that actually is what happens.
4:36Script-- it says give me two numbers.
4:37I'm going to say number 1--
4:39oh, my VM got away from me there.
4:41Let's clear the screen.
4:42All right, try this again.
4:43Script-- give me two numbers.
4:44I'm going to give you number 12 and number 65,
4:48and the sum is 77.
4:50We can make that a little more user friendly.
4:53We could say echo, "the sum is" and so now give me
4:58two numbers--
4:5856 and 222.
5:02The sum is 278.
5:03So that's really cool.
5:05One of the things--
5:05remember I said back ticks?
5:07I wanted to show you how back ticks work because,
5:09what if we wanted to assign the sum to another variable?
5:14What we can do is say, sum equals, and then
5:19if we put this command in back ticks--
5:23now the back tick is the key to the left of the 1 key,
5:28if you're on a US keyboard.
5:30But a back tick means basically, I
5:32want you to execute whatever is inside this back tick,
5:35and that is what I want you to assign to this.
5:38Or I want you to just do this, replace
5:41whatever is between the back ticks with the results
5:43of what's in here.
5:45All right, so what we've done is we've assigned sum--
5:48or we've assigned this to the variable sum.
5:52And then we can say echo, dollar sign, sum is the answer.
5:59So what we should see now is the same thing, except it will say,
6:02"the sum" and then "is the answer."
6:05Let's see how that works.
6:07Script-- give me two numbers--
6:085 and 6.
6:11The sum is, 11 is the answer.
6:12Now that's because I left this one in,
6:14where it says the sum is, but it did--
6:16it actually assigned it to that variable sum
6:19and then it said 11 is the answer.
6:21Does that make sense, what it's doing there?
6:23We could also, instead of saying read variables,
6:25we could change these and instead of read NUM 1,
6:29read NUM 2, we could say expr dollar sign 1, dollar sign 2
6:34and have them be the arguments that we've given
6:36the script on the command line.
6:38So there's lots of things that you can do in order
6:41to manipulate variables inside a script,
6:44but it's really important to understand how to assign them.
6:47And that's one of the things that got me,
6:49there can be no space between the variable name
6:52and the equal, otherwise it doesn't assign it properly.
6:55And these back ticks can be really useful
6:57because you can put anything in here.
6:59If you wanted to put the command LS in there,
7:02then it will assign the entire string of the output of LS
7:05to a variable, and you can do something
7:07with that variable later.
7:08So I encourage you to play around with variables
7:11as much as you can, and we've learned to assign them
7:13in multiple ways.
7:14We can assign them inside the script,
7:17we can assign them by reading from the command line,
7:19and we can actually use the dollar sign 1, dollar sign 2,
7:22dollar sign 3, to get them from command line arguments,
7:25as well.
7:26And depending on the script you're doing,
7:28all of those various things can be extremely
7:30useful on your scripts.
7:33And I've been making shell scripts
7:34for a very, very long time.
7:36And I have to say, the most common way that variables are
7:39assigned-- usually for a sysadmin purpose--
7:42are when you use them as arguments,
7:44like that dollar sign 1, dollar sign 2.
7:45It makes it very flexible.
7:46You can say whatever your script is, and then
7:49say you want to ping an address, or you
7:51want to SSH to it, you can actually just
7:53give it arguments on the command line,
7:56rather than having it read in.
7:58If it's waiting for you to prompt,
7:59that's another step that's kind of a pain.
8:02And you can't use it in a Cron job or something,
8:04to automate things, if it's waiting on the command
8:07line for you to read.
8:08So it's important to know that you can read things
8:10in from the command line, but these arguments-- command line
8:13arguments-- are very powerful.
8:14And that's usually how you get data into a shell script.
8:17Also back ticks are important.
8:19Hopefully you understand what was going on there.
8:21And assigning variables inside a script is also useful.
8:25But remember that dollar sign 1, dollar sign 2 thing,
8:27because that's going to be extremely, extremely useful
8:30as you create your own files.
8:31And I encourage you, play around with assigning variables,
8:34and echoing, and all that stuff, so you
8:35get a feel for how to assign variables and manipulate them
8:39inside your bash scripts.
8:40I hope this has been informative for you
8:42and I'd like to thank you for viewing.
Scripting: BASH Conditionals — Intermediate
0:00One of the problems with using BASH for automating tasks
0:03is that when a human is doing tasks,
0:06you can determine what needs to be
0:07done based on different conditions
0:09or different circumstances.
0:11Thankfully with BASH, we can program some logic
0:14so that things happen based on other things.
0:17So we can say, if this, then do this, else do that,
0:20things like that.
0:21And we can use various conditionals.
0:23We can use file tests, like is a file there?
0:26Is it not there?
0:27Compare is value 1 bigger than value 2?
0:30Does this string equal that string?
0:32There's lots of things we can test in order
0:34to figure out if this certain conditional circumstance
0:38exists, then do this.
0:39And conceptually, it's logical, right?
0:42I mean, it's the way that we think.
0:43If we happen to be this young adventurer
0:46and we're going along on our path, we get to a point
0:49where there's a monster off in the distance.
0:51And we can say, if we have a sword,
0:55then go battle the monster.
0:57If we don't have a sword, then we just
0:58keep going straight through.
1:00So this is a conditional.
1:01We're going along the path.
1:02Hey, do I have a sword?
1:03Yes.
1:04Go battle the monster.
1:05Do I have a sword?
1:06No.
1:06All right.
1:07Then just keep going through, otherwise the monster
1:09will eat you.
1:10So if-- we'll just say yes or no.
1:12You can do it or not do it.
1:14But there's another step we can take.
1:16We could be traveling along this same path
1:18and we get to this point.
1:19And it says, if you have a sword,
1:22then go fight the monster, else go play LEGOs.
1:26And then when you're done playing LEGOs,
1:28then you continue on your path.
1:29So this gives you a choice where you
1:31have to do something at this point in your adventure.
1:35If you have a sword, you go battle the monster.
1:37If you don't have a sword, then you go play LEGOs.
1:40But the point is, this conditional statement
1:43can't just be bypassed.
1:45There's a condition where if this is true, then you do this.
1:48If it's false, then you do this.
1:50And there is no circumstance where you don't do anything.
1:53So you can have an if statement without an else.
1:55But if you add an else, all of a sudden you're
1:58doing one or the other.
2:00So this would be the "then" part of it,
2:02and then this would be the "else" part of it.
2:04And this works really easily in BASH.
2:06But it can be a little confusing as
2:08to how to frame these test conditions,
2:11like what the syntax in BASH actually is.
2:13So we're going to look at that on the command line.
2:15But I wanted you to understand conceptually what's
2:17going on when we make these choices in a program
2:20or in our adventure.
2:22Now, I learn best by doing, so let's create a script
2:25and learn how to create it along the way.
2:26First, I'm going to show you a little trick.
2:28If I type ls, you'll see we have no scripts in here.
2:31A lot of times what I'll do is I'll say echo.
2:33And then in single quotes, I'll put my shebang.
2:37And then I'll redirect that into a file called namegame.
2:42And then I'm going to chmod plus x, namegame.
2:47And now if I vi namegame, what I get here--
2:50the reason I did it on the command line
2:52is all of a sudden, since I've done
2:53that, now I get syntax highlighting, which
2:56is convenient.
2:57Otherwise, the first time it opens,
2:59if it doesn't see the shebang statement,
3:01we don't get syntax highlighting.
3:02So anyway, that's just one of the tricks that I do.
3:04But first of all, let's make a script that
3:08compares two different names.
3:10Now, I'm going to type this out and speed it
3:12up, but feel free to follow along and type it out as well.
3:15I didn't include it in here, because I
3:17wanted you to type it out.
3:19Something about physically typing out a command
3:21makes it stick into your head easier.
3:23So first of all--
3:32OK.
3:32So I typed this out.
3:33And basically, the logic goes like this.
3:35"My name is Shawn"--
3:36I set it to the string Shawn.
3:38And then it says "My name is Shawn.
3:39What's your name?"
3:40And then it reads the variable YOURNAME from the command line,
3:43so we'll have to type that in.
3:44And then it does this.
3:45It says if and then open brackets--
3:48inside here is the test condition.
3:50So when we say if, we say if this stuff
3:52inside these brackets is true--
3:55and the next line says, then.
3:57And it does everything after then, until it gets to fi.
4:01So fi, why fi?
4:02Well, it's just if backwards.
4:04I don't know why they've done it that way.
4:06So if and fi.
4:07That's how you can tell this is an entire if statement.
4:11So it's going to compare those names.
4:12Let's save this and execute it and see how it works.
4:16namegame.
4:17"My name is Shawn, what is your name?
4:19"Bill."
4:20Nothing happens.
4:21Let's run it again.
4:23namegame.
4:23"My name is Shawn, what is your name?"
4:25"Shawn."
4:26"We have the same name."
4:28It worked.
4:28Yay!
4:29Now unfortunately, at first when I said my name was Bill,
4:32we didn't get any response.
4:33And that's not any fun, really.
4:34So why don't we go back and edit this file?
4:37And we can add an else statement.
4:39Remember we talked about with the monster and the LEGOs?
4:41Let's add the LEGOs part of this.
4:43So we can say then echo that, else echo
4:49"we do not have the same name.
4:53Sad day."
4:54And so now, no matter what we type, we should get a response.
4:58Now I want to point out this indentation.
5:01BASH scripting doesn't care about indentation at all.
5:04I only indent here to make it more readable.
5:07So everything inside then-- because we
5:08can have more than one.
5:10"That is so cool."
5:14So everything in between then and else or then
5:18and fi if we don't have an else statement is executed.
5:20And I indent them just to make them more readable.
5:22But we could mash them all on the front.
5:24It doesn't really matter at all.
5:26All right.
5:26So let's save that.
5:28And then we'll play the name game again.
5:29namegame.
5:31"My name is Shawn, what is your name?
5:33"Bill."
5:33"We do not have the same name.
5:35Sad day."
5:36But what if my name is Shawn?
5:38"We have the same name!
5:39That is so cool."
5:40All right.
5:41So you can see logically what goes on.
5:44And we don't have to have it prompt us.
5:46Just like with our variable Nugget
5:48where we learn how to assign variables,
5:49this could be anything inside of our script
5:52that uses for comparative's sake.
5:54And speaking of comparative, that is where this came from.
5:58So the conditional tests.txt, I included it in this lab
6:02so we can look at it.
6:03And you'll see there are all sorts of things you
6:05can put inside the brackets.
6:08So if we're doing like comparison operators
6:11for numbers, we can use things like dash eq.
6:13And we would do it like this.
6:15We say this variable is equal to this variable.
6:20Now remember, BASH treats all variables as strings.
6:23However, if you use a numerical comparison
6:26like dash eq, dash ne for these numerical things,
6:29it will use numbers as integers.
6:32So you can assign numbers to these
6:34and it will actually compare them as integers
6:37if you use these types of comparisons.
6:40Now, we already did this.
6:41We already did a string comparison,
6:43where we said if it equals, then they are the same thing.
6:46But there are other things you can do.
6:47We can use equal.
6:48We can use not equal.
6:50We can use dash z.
6:51If the string is null, like if it doesn't contain anything--
6:55if we do a typo over here and like we
6:58use a variable that doesn't have anything assigned,
7:00then it would be null.
7:01And this test would do that.
7:03If we would do that, we would just say in brackets,
7:06dash z string1.
7:07Is string1 one null?
7:09And if it is, then it would be true.
7:10Is the string not null, using -n.
7:13And then, this is what I want to finish the Nugget
7:15with are these file tests.
7:17How this works is you can say dash a does the file exists?
7:21Dash e, does the file exist?
7:23These are the same, a and e.
7:25d.
7:26Does it exist and is it a directory?
7:29File exists and it's a regular file.
7:31It's not a directory or a symbolic link.
7:33The file exists and it is a symbolic link.
7:35So these tests are inside brackets
7:39and it's based on a file.
7:40Now, this is cool too.
7:41What we can test can be a file path,
7:43like the full path to the file, or it
7:45can be a variable that has the path assigned.
7:48Now, what do I mean by that?
7:49Let's make another script and we can see it in action.
7:52Now, I'm going to do the same thing.
7:53I'm going to say echo in single quotes bin/bash.
7:57We're going to call this file toggle.
7:59chmod plus x toggle.
8:02vi toggle.
8:03All right.
8:04So here we have this.
8:06Now what I want to do is I want to do some file tests.
8:09So let's assign a variable, MYFILE equals current directory
8:15forward slash thefile.txt.
8:19We don't have to say dot txt, but I did.
8:21So the variable MYFILE is assigned this.
8:25It's the path, the complete path to a file,
8:28current directory, thefile.txt.
8:30So our if statement now is going to be--
8:36So can you figure out what's going on here?
8:38We have we're going to set the name again of MYFILE variable
8:41to this path.
8:42We're going to say, if it exists, if MYFILE exists,
8:46then remove the file.
8:48Else, if it doesn't exist, touch my file.
8:51In other words, it's going to toggle creating and deleting
8:54depending on if it's there.
8:55If it's there, it's going to delete it.
8:57If it's not there, it's going to create it.
8:58So let's see how this works.
9:00We'll type ls.
9:01We do not have MYFILE or whatever,
9:03so we're going to say toggle.
9:05I'll type ls.
9:06Look, thefile.txt.
9:08It's right there.
9:09Let's run it again.
9:12Ha!
9:13It's gone.
9:14It deleted thefile.txt.
9:16We run it again, it's going to toggle.
9:18And sure enough, it's there.
9:20Run it again, it's gone.
9:22So what we've done is we've created a conditional statement
9:25that will toggle this file's existence on and off.
9:29And here is where it gets fun.
9:30You are in this lab.
9:32I want you to play with all of these things.
9:34See if you can figure out how to use a symbolic link.
9:37See if you can determine if a file is executable or not.
9:41So we could say, is this script executable?
9:43If not, then chmod it so it is executable,
9:46et cetera, et cetera, et cetera.
9:47There's all sorts of things you can do and play
9:49with these conditionals and comparisons
9:51and create your own scripts to do all sorts of things.
9:54Combine this knowledge with our variable usage,
9:57so we can do things like that name game that we just did.
9:59Anyway, this is a really great way to learn,
10:02and that is just to get in here and write your own scripts.
10:05And I gave you this cheat sheet, so you
10:07have a whole bunch of information
10:08on how to form these comparison or these conditional
10:12statements, so that you can get some awesome results out
10:15of your scripts.
10:16So your assignment is easy.
10:18Using the things that we've learned here,
10:20if, then, else, file tests, comparisons, et cetera,
10:23go play.
10:24I want you to play, play, play with BASH scripting in your lab
10:28file.
10:28In the lab, just play and do all those things
10:30so you get the hang of how to make those if statements.
10:33You don't have to have else, but you can have else.
10:36Play with having them or not having them.
10:37How does it work?
10:38How does it not work?
10:39Does something not work?
10:40And if it doesn't work, can you figure it out?
10:42That's the fun of learning and that's
10:43what I want you to do from here on out.
10:45I hope this has been informative for you,
10:46and I'd like to thank you for viewing.
Scripting: Nested Conditionals and Case Statements — Intermediate
0:00Now, sometimes when you're dealing
0:01with conditional statements in BASH,
0:03there's more than just a yes or no option.
0:06You have multiple conditions.
0:08In that case, there are several ways--
0:09there's multiple ways-- you can deal with it.
0:11You can either nest your if/then/else statements,
0:14or you could use the elif, which is like else if,
0:17or you can use a case statement.
0:19There's lots of ways to handle situations like that.
0:21We're going to quickly look at all of them.
0:23Because every situation, one, is going to make more sense,
0:26or you're just going to have personal preferences when
0:29it comes to scripting as to what makes the most sense.
0:31But first, let's get our scenario down.
0:34Here we have Dance Party 2017.
0:37And there were three people at this awesome dance party.
0:40There was Bob, Tom, and Martha.
0:43So our script is going to try to determine
0:46if the user of the script happened
0:48to be at Dance Party 2017.
0:51So just keep this in mind.
0:53There were three people at Dance Party 2017.
0:56And we're going to see if the people running the script
0:59happen to be one of the people at the party.
1:01So let's go to the command line.
1:03For this Nugget, I did leave the conditionaltests.txt in there
1:06for your reference while you're making your own script.
1:08But let's start out by using my trick--
1:10echo in single quotes, do the shaband.
1:16And then let's edit this party file.
1:19And we'll start out-- now, first of all,
1:21let's say what is this person's name.
1:23Because we want to figure out if they're at the party.
1:25So we're going to say echo, what is your name.
1:29And then we're going to read in the variable name.
1:32So we have that variable.
1:33Now we're going to say if name equals Bob,
1:40then you might have been at the party, Bob.
1:49Else echo, you were probably not at the party, name,
1:59unless you lied about your name.
2:04And then FI to close out that statement.
2:06So here's the deal.
2:07It says what's your name.
2:08And if your name is Bob, it's going
2:09to say you might have been at the party, Bob.
2:11But what if Tom or Martha put their name in there?
2:14Well, with what we know now, we're
2:16in trouble, because we can't say if name is Bob, Tom, Martha.
2:21So what we can do is add an or thing to this.
2:24So if we do space pipe pipe-- so the pipe symbol is above
2:28the Enter key there--
2:29we can add another condition that it has to meet
2:32or that it can meet.
2:33So or means this or that.
2:36We could actually do and and.
2:39And that means that these two conditions
2:41both have to be true.
2:42But I'm going to stay with or.
2:44And then we do another conditional statement.
2:47So name equals Tom space close bracket.
2:53This is something I want to mention.
2:54Notice I always put a space after here
2:57so there is a space between Tom and the bracket.
2:59The reason I do that is because BASH
3:01can be really picky about thinking that you were trying
3:04to compare it to Tom bracket.
3:07So it needs that space in there in order
3:10to really make sure that it knows
3:12that you're closing it off.
3:13So it's best to put spaces inside the brackets
3:16so that BASH doesn't get confused.
3:18Now, I'm also going to add another or.
3:20And I'm going to say or name equals
3:25Martha space, close bracket.
3:27So now the logic is if your name is Bob, Tom, or Martha,
3:33you may have been at the party, so and so.
3:35Otherwise, you were probably not at the party,
3:37so and so, unless you lied about your name.
3:39So let's see if that or statement works.
3:41We'll say party.
3:42What is your name?
3:43Mike.
3:44You were probably not at the party, Mike,
3:46unless you lied about your name.
3:48Well, Martha.
3:50You might have been at the party, Martha.
3:52How about Tom?
3:53You might have been at the party, Tom.
3:55How about Tommy?
3:57You were probably not at the party, Tommy,
3:59unless you lied about your name.
4:01So again, the computer doesn't know that Tommy and Tom
4:04might be the same person.
4:05But you can see how the logic works
4:07using the or statement for multiple conditions.
4:10Now, there is a different way we could have gone about this.
4:13So let's edit party, and let's say we didn't want to do these
4:16or conditionals.
4:21So here's our if your name is Bob,
4:23then you might have been at the party, Bob.
4:25But what we could do is we could nest a statement.
4:28So we could say else if name equals
4:34Tom, then echo you might have been at the party.
4:40Actually, here we don't even have to use the variable name.
4:43We could just say Tom.
4:44Because we know that this is only checking for the name Tom.
4:46So let's say you might have been at the party, Tom.
4:49So this is now a nested if statement
4:53inside the already-existing one.
4:56So we could say, if the name is Bob,
5:00then you might have been at the party, Bob.
5:02Else if your name is Tom, then, echo,
5:04you might have been at the party, Tom.
5:06But what about Martha?
5:08Well, then we need to actually do this.
5:09We need to add another line and say else
5:13if name equals Martha, then--
5:20you notice it's starting to get really, really complicated--
5:23echo, you might have been at the party, M Dog.
5:31OK.
5:31Does this make sense now?
5:33We say if your name is Bob, you might have been at the party.
5:35Else, if your name is Tom, then you
5:36might have been at the party.
5:37Else, from this if statement, if your name is Martha,
5:41then you might have been at the party, M Dog.
5:43And then we have to close off each one of those
5:45nested if statements with FI.
5:47Now, this should functionally do something very similar.
5:50And we'll test it very quickly.
5:51What's your name?
5:52Bob.
5:53You might have been at the party, Bob.
5:54What's your name?
5:56Tom.
5:56You might have been at the party, Tom.
5:58What's your name?
5:59Martha.
5:59You might have been at the party, M Dog.
6:02And what's your name?
6:03Mike.
6:04Nothing.
6:05Oh.
6:05Why did it do nothing?
6:07What is your name, Mike?
6:08Well, let's look at our script.
6:10We didn't do any sort of error or any other else
6:15with this one.
6:16So if it gets all the way to it, it says your name is Bob, no.
6:20Then if your name is Tom, no.
6:22Then if your name is Martha, no.
6:24But notice I didn't put an else say anything about you
6:26weren't at the party.
6:27So this incredibly complicated script isn't as powerful
6:31as that original one was.
6:32But logically, you can follow what's going on.
6:35And that's exactly what I wanted to get at.
6:37Sometimes, you can nest these if then statements just like this.
6:41Now, there is another way that you
6:42can nest the same information.
6:44Instead of saying if then else, if then else, if then else,
6:48what you can do is say if name equals Bob, then echo this.
6:52And here's how we can change it.
6:54We can say elif--
6:56just kind of combine this on one line--
7:00else if name equals Tom, then they
7:03might have been at the party.
7:04Elif name is Martha, then your name
7:08might have been at the party.
7:10And notice how these are highlighted in red.
7:12Else if, or elif, becomes part of that same initial
7:15if statement.
7:16So we don't need to close off all of these other
7:18if statements with FIs.
7:20So it's the same exact thing, but hopefully this
7:23is a little bit clearer.
7:24What's your name, name?
7:25If name equals Bob, then echo you might
7:27have been at the party, Bob.
7:28Else if your name is Tom, then echo
7:30you might have been at the party, Tom.
7:32Else if your name is Martha, then
7:34echo you might have been at the party, M Dog.
7:37And now we could add else echo didn't see you there.
7:44So we can say if, elif, elif, else.
7:47So you can have as many elifs as you want, and it just
7:50keeps going down the line, and then we close with else.
7:52So if none of those are true, then we still
7:54get didn't see you there.
7:56So let's save this--
7:58party, what's your name, Mike.
8:01Didn't see you there.
8:02What's your name?
8:03Tom.
8:03Might have been at the party, Tom.
8:05So it works just like the other one,
8:07and it's a little bit clearer than all that nested stuff.
8:11In fact, it's a lot clearer than that if then else,
8:13if then else, if then else, 5, 5, 5, 5, 5, 5, 5 at the end.
8:16So elif is a great way to do that.
8:18But there is one more way I want to cover--
8:20and then we'll be done-- and that is a case statement.
8:23And case means you take the results of something,
8:26and rather than if then else or if else elif, you say here's
8:31my variable.
8:32Match it up to one of the results below.
8:33So let's look at this again, VI Party
8:36So we're going to start out the same.
8:38We're going to say what's your name, read your name.
8:40And then I'm just going to get rid of all this,
8:42because it's going to look very different with our k statement.
8:46Now, I'm just going to type this entire section out,
8:49and then we'll cover it after the fact.
8:51Because there's a lot of text going on,
8:53and I'd rather type it out and then
8:54go through it by hand afterwards.
8:58This looks more complicated, but it actually
9:00makes it easier if you have a long list of options
9:02that you want to cover.
9:03So here we get the name assigned,
9:05the name of the variable is assigned.
9:06And then we say case name in.
9:09And then we say what we want this condition to be--
9:12Tom.
9:14And then we put a close parentheses.
9:16So this is the option close parentheses.
9:18What we want it to do-- it can be multiple lines.
9:21And then we stop it with semicolon, semicolon And then
9:23the next option--
9:24Martha with a close parentheses, the stuff
9:27we wanted to do there closed with the double semicolons,
9:30Bob, semicolons.
9:31And then at the end, rather than an else statement,
9:34this is called the default option, just asterisk.
9:37So if it's anything, then it does this.
9:40Because once it does--
9:42if Martha is it and it does this,
9:44it only executes this one, and then it goes to the end.
9:46It doesn't keep going down the list.
9:48As soon as it meets one of the criteria,
9:50it executes the thing in that section and then stops.
9:52So if it gets to the very end, it
9:54will execute this one, because this matches everything.
9:57Asterisk matches everything that might be left,
9:59and it says you weren't there, dude.
10:01And then just like the if and FI, we
10:03end a case statement with esac.
10:06Because esac is case backwards.
10:08So let's test this really quickly.
10:11What's your name?
10:12Tom.
10:13You weren't there, dude.
10:14Why does it say that?
10:15Because I put a capital O. Whoops.
10:17Party.
10:18Tom.
10:19Hi, Tom.
10:19You might have been there.
10:21Party.
10:22Bob.
10:23Glad you could make it, buddy.
10:24Martha.
10:25Hey, Martha.
10:26You can really dance.
10:27Oh, I actually meant you can really dance.
10:29But here, it said can you really dance.
10:31Anyway, it recognized Martha.
10:33And of course, Mike wasn't there.
10:35You weren't there, dude.
10:36So you can see that there are multiple ways
10:38to accomplish the same thing.
10:41But depending on your circumstance,
10:42it might be easier.
10:43So what I encourage you to do-- look at the script
10:45that we've done, the different scripts we've done in this,
10:48and make yourself a very complicated
10:50multiple conditional argument or statement,
10:53and see if you can make it work the way you expect it to work.
10:57Does that make sense?
10:58I want to see if you can make it-- not just
11:00our Bob, Tom, Martha scenario, but whatever scenario you have.
11:03Maybe it includes arithmetic.
11:05Maybe it includes $1, $2, $3 variables
11:09that you do right on the command line.
11:12Mix the things that you've learned
11:13and experiment so that you get the hang of how
11:16conditional statements work, even if they're
11:18nested or really long, multiple conditionals, et cetera,
11:22et cetera.
11:22And really, all there is left to do is play.
11:25Just go in and try to do all of the things
11:27that we demonstrated.
11:28Do multiple conditionals.
11:29Do nested if then elses.
11:31Do elif statements.
11:32See how many you can string together.
11:34Do a case statement if that makes more sense to you.
11:36A lot of these things are different ways
11:38of doing the exact same thing.
11:40However, when you understand how they all work,
11:42you can make the best decision for whatever script
11:45you happen to be writing.
11:46I hope this has been informative for you,
11:48and I'd like to thank you for viewing.
Scripting: BASH While and Until Loops — Intermediate
0:00Now, the whole idea of using bash scripts on your system
0:03is to make repetitive tasks easier
0:05to automate the process of doing the same thing over and over.
0:09And that's where loops really come into play,
0:11if you need to do something more than one time.
0:13Now loops are really cool.
0:14In this Nugget we're going to talk about while loops
0:16and until loops.
0:17They are very, very similar, except that they're completely
0:20the polar opposite.
0:21I'll show you what I mean when you get to the command line.
0:23But the cool thing is loops like while and until work
0:26on conditional statements.
0:27So it'll keep looping until conditions are met.
0:30You can also increment things.
0:31It's possible to make endless loops which you may want to do
0:34but sometimes-- actually usually--
0:36endless loops are an error, so it's
0:38important to know how to get out of those.
0:40But let's go right to the command line
0:42and make some loops so that we can see how beneficial they
0:44can be to our scripting world.
0:47Now first I want to demonstrate an incremental loop.
0:49So let's create a loop or let's create our file.
0:53We'll call it loopy.
0:54And let's edit that.
0:56All right so inside here we have our file.
0:58Now what I want to do is have a script
1:00that will count from 1 to whatever we input.
1:02So I'm going to start out by saying
1:05x equals 1 all right so that's our variable to start with, x.
1:10Now I want to say echo "Give me number."
1:15Read "Num."
1:16Echo "Cool, I can count to Num, check it out."
1:23Now we need to create our while loop.
1:25So how we do that it was to say while and then open brackets.
1:28Just like an if conditional statement,
1:30this is going to be a conditional statement.
1:32So while this stuff in the brackets is true.
1:35So while Num is g.t.
1:38greater than $x, "do".
1:42So it's going to repeat this loop as long as number
1:46is greater than x.
1:48All right so first let's start out.
1:49What are we going to do?
1:50First, we're going to say echo x.
1:53Next, we have a command that we haven't used yet.
1:56It's called "let".
1:57Now let is a way to assign a variable using mathematics, OK.
2:00And I'm going to show you what I mean.
2:02We can say let x equal x plus 1.
2:07All right so let x equal what it is plus 1.
2:11Now if we were to just assign--
2:13if we were to just say x equals x
2:15plus without the let statement, it
2:17would equal the string x plus 1.
2:19And that's not what we want.
2:20We want it to do the math.
2:22So we have to use this let statement.
2:24So let x equal x plus 1, done.
2:27Now I don't think this is going to work quite right, let's see.
2:30So we'll save this file.
2:32Let's run loopy.
2:33And it says give me a number, we're going to say 10.
2:36Cool I can count to 10, check it out.
2:381,2,3,4,5,6,7,8,9.
2:42What went wrong?
2:42Well this is where troubleshooting comes into play
2:45and we can see what happens.
2:46It starts with x equaling to 1.
2:49OK, and then it says x equals x plus 1.
2:53When it gets to 9--
2:54when it says echo 9, it says let x equal x plus 1.
2:57So then x equals 10, it goes back up to here
3:00and it says while 10 is greater than 10.
3:04See it's not anymore, so it never prints out 10.
3:07Now we could solve that.
3:09We could say all right, you know what?
3:11x equals 0 and let's do this.
3:17Let's say let x equal x plus 1 before we echo.
3:24And now let's see if that makes sense logically.
3:26So it starts out as zero.
3:27It says cool, I can count to 10--
3:29if we put in 10-- check it out.
3:31And it says while 10 is greater than zero, OK,
3:34so it's going to go through.
3:35It's going to add 1 to it so it's going to say 1.
3:38And then when it gets to 9--
3:40So echo 9-- it goes through the top.
3:43It says while 10 is greater than 9 sure enough.
3:46So then it's going to add 1.
3:47It's going to say echo 10.
3:48So by switching this around we should
3:51have corrected our error.
3:52Let's find out.
3:53Loopy, give me a number 10.
3:551,2,3,4,5,6,7,8,9,10.
3:57Sure enough it counted all the way to 10 just
3:59by switching around our logic.
4:01And that's really important when you're
4:03doing these conditional statements
4:04to think through your logic.
4:06Now the other kind of conditional statement
4:08I want to show you.
4:10We'll use the same script, we'll just edit it out.
4:12I didn't mean to do that, I can count to two.
4:15Vi loopy.
4:16All right, so the next thing I want to do
4:18is instead of using arithmetic--
4:20Let's get rid of all the stuff in here--
4:23I want to use a text comparison.
4:25So let's play a game and we'll set some variables here.
4:30We'll say best equals pizza.
4:34And then we'll say echo "Can you guess the best food?"
4:40Read "Guess".
4:43And now let's do a loop.
4:45So it's going to enter in this variable guess
4:47and we're going to say maybe somebody puts in chicken.
4:50Well now we're going to say I want
4:51you to run the following loop until they guess correctly.
4:55So until-- open a bracket, this is a conditional statement
4:58just like an if statement.
5:00So until guess is equal to--
5:03Now you can use one equal or two.
5:05When I'm comparing things I like to use two
5:07equals because other programming languages generally do that,
5:10but you can use one or two equals in this case--
5:13best.
5:14So until guess equals best, I want you to do the following.
5:18I want you to say echo "Guess again"
5:21and then read "Guess" done.
5:24And then once they do finally guess it, echo "Yay!
5:28You guessed the right answer!"
5:31So let's see if we can follow this logic.
5:33Well first of all we said that pizza is the best food.
5:36Then it says "Can you guess the best food?"
5:37It asks for a guess and that initial guess is given a value.
5:44And then it says it's going to run the loop until guess
5:49is equal to best.
5:50Now if they guess on the first one.
5:53If they type in pizza here it's never
5:55going to run this loop at all because it's
5:58going to say I want you to run this until these two equal
6:01each other.
6:01Well if they equal each other initially
6:03it's never going to execute this loop at all.
6:06It's just going to go right down to here and say "Yay!
6:08You guessed the right answer!"
6:09However, if they guessed wrong it's going to start this loop
6:12and it's going to keep going through these two things
6:15until they match.
6:17Does that make sense?
6:18Hopefully it does.
6:19Let's see how it works.
6:20Save this file.
6:21Say loopy "Can you guess the best food?"
6:24Chicken.
6:25Guess again.
6:25Fish.
6:27Turkey.
6:27No.
6:28Pizza.
6:29"Yay!
6:29You guessed the right answer!"
6:31So you can see it just keeps doing the loop where
6:33it says guess again, guess again, guess again, until they
6:37enter the exact same string that we
6:40set at the beginning of that script.
6:42Hopefully that makes sense.
6:44Now you may notice I showed you how to do
6:45while loops and until loops.
6:47We learned about how conditionals
6:48work just like they do in the if statement
6:50and we learned how to increment using the let command.
6:53I did not mention endless loops or I didn't go over that.
6:57That's what you need to do.
6:58Go in and see if you can make a loop that never ends.
7:02It just keeps going, and going, and going, and going.
7:05And what you need to do when that happens
7:06is hit Control C on your keyboard.
7:09That's the magic.
7:11Control and then the C key will break any endless loop
7:15that's going on.
7:16You may have to hit it a couple times if it's
7:18like echoing over, and over, and over it might not
7:20realize you hit control-c.
7:21So just keep doing control-c, control-c, control-c.
7:24Eventually it will stop the script and you'll get back
7:27and then you can edit it and see what you can fix.
7:29Again, you are assigned to this as your task.
7:33Try to create an endless loop but play
7:35with all these conditionals and incrementing
7:37and all that sort of stuff.
7:38It's fun to play with loops.
7:39While loops and until loops are pretty much
7:42the exact same thing, except one functions while it's true
7:46and one functions until it's true.
7:48And hopefully that makes sense.
7:49I hope this has been informative for you
7:51and I'd like to thank you for viewing.
Scripting: BASH FOR Loops — Intermediate
0:00The for loop is a really cool loop
0:02when it comes to using bash loops
0:03because it doesn't use conditional based testing.
0:07So it's not like it's as if this is true,
0:09then stop, or if this is true, then keep going.
0:11You can do a bunch of things with a for loop.
0:13You can use sequences of numbers.
0:15So you can say, do a loop 10 times,
0:18and it'll go 1 through 10.
0:19And you can also do-- and this is really what makes it
0:22special--
0:22an unrelated set of data, you can turn into a loop.
0:26So what a for loop does, is it takes a set of data--
0:29and again, it could be a sequence,
0:30or just a random chunk of data, file name, stuff like that--
0:33and it loops through every single possibility
0:36in your set of data.
0:38So what do I mean by that?
0:39Well let's say that we have two different types of things.
0:41We have a filing cabinet here filled
0:43with files that are numbered from 1 to 1,000.
0:46With a for loop, you can say, all right.
0:48I have 1 through 1,000 files, and I
0:52want you to then go one, two, three, all the way to 1,000,
0:56and then do something to every single one of those.
0:58And that we could accomplish with a while or until loop.
1:01The cool thing is, when you take a situation like this and say,
1:04OK.
1:05On my wall, I have a pair of pliers, a crescent wrench,
1:07a hammer, and a saw, and I want you to do something
1:10with everything on the wall.
1:11Well even if you don't want to go through and do it one by one
1:15by one, you can say, take this whole set of data--
1:17whatever it happens to be--
1:19and then go through every item on the list
1:21and do something to that.
1:23So it means that you're no longer restricted
1:25to just numbers or a conditional test
1:27for text-- you can actually take any set of data,
1:30and do some task to every item in that set.
1:33So let's go to a command line and look
1:35because it's a lot easier once you get the feel for what's
1:38going on, and it's so powerful.
1:41It's just awesome.
1:42All right-- first thing, let's create our file.
1:44So like I normally do-- echo, single quotes, the shebang.
1:48Then bash, into "for loop," we'll call it.
1:51And then we're going to chmod that, plus x, so that we
1:54can execute it directly.
1:55All right, now let's open this up and create our loop.
1:59Now the format for a for loop is "for x in,"
2:04and then whatever the data set is.
2:06The data set can be anything-- it
2:08can be a sequence of numbers, it can be files.
2:10So let's do it simply-- say 1, 2, 3, 7, 8, 9, do, echo,
2:18number, x, done.
2:21So this is the form, and let's look
2:22quick before we execute it.
2:24So "for x in," and what it does is
2:26it takes whatever data set you specify after this,
2:29and it can just be a data set typed out,
2:31or it could be a range, which we'll do next,
2:33but we'll say "for x in," and then what it does
2:35is it assigns every data item to the variable,
2:39and it goes through and does it like that.
2:41So what we should see is, number 1, number 2, number 3,
2:44number 7, number 8, number 9 as it goes through.
2:47And notice how it starts and stops each loop.
2:50We can put anything in between do and done,
2:52and those are the things it's going
2:54to do every single iteration in the dataset.
2:58So let's save this file, and execute it, and we should see--
3:02sure enough--
3:04number 1, 2, 3, 7, 8, 9.
3:06Now we don't have to spell each one out.
3:08We can use a range, and how we would do that is--
3:13get rid of this-- and now we're going
3:14to say, open the curly braces.
3:17Say 1 dot dot 10, for example, close the curly braces,
3:22and now this is going to do this for a set range.
3:26So it's going to, instead of typing 1 space, 2 space, 3
3:29space, 4 space, 5 space--
3:31this is going to do the same thing,
3:32but it's going to give us a range of 1 through 10.
3:35And quickly look at this, we'll save the file,
3:37we'll execute for loop, and we should see number 1 through 10.
3:40Sure enough, number 1, number 2, number 3,
3:42all the way through number 10.
3:44And if you have version four or later,
3:46which you almost certainly do of bash,
3:49there's this other cool thing.
3:50We can do a step counter of something here.
3:54And now this looks a little confusing,
3:55but it's doing the same thing.
3:57It's saying a range of 1 to 10, but I
4:00want you to count by twos.
4:03All right, so the range is still the first part,
4:05but then another dot dot, and then the step counter
4:08means what you count by.
4:10By default, it'll count by one, but we're
4:12going to say count by two.
4:15Now if we do this and save it, it goes 1, 3, 5, 7, 9.
4:20Now why didn't we see 10?
4:21Well because it counts by two, so it says it starts with 1,
4:25and it says adds 2, 3, adds 2, blah, blah, blah.
4:27So we get to 7, it adds 2, gets to 9, it adds 2,
4:31and it's 11, which is no longer in our range from 1 to 10.
4:36So even though we said go from 1 to 10, when we count by twos,
4:39we never get to 10, so we don't see number 10 here.
4:43We just see 1, 3, 5, 7, 9.
4:45And that's how the range variable works.
4:47But we don't just have to use numbers,
4:50which is really, really cool.
4:52All right, let's open this up again.
4:54And now we can go here, and instead
4:57of typing numbers or a range so it calculates that,
5:01we can say fish, chicken, puppy, tuna.
5:05So this is just a list of things--
5:08again, it's a dataset--
5:10a number of things in our dataset.
5:12But it's not a range, it's not a number-- this just
5:14happens to be four strings, and it's
5:17going to loop through this set of data for us.
5:20Now it's no longer going to be a number.
5:21Let's say, "look it's a," and hopefully, it
5:25should say, look it's a fish, look it's a chicken,
5:27look its a puppy, and look it's a tuna.
5:29Let's see if that's what happens.
5:31Sure enough, look it's a fish, look it's a chicken,
5:33look it's a puppy, and look it's a tuna.
5:35So you don't have to use a range,
5:38and it's not incrementing.
5:39Also remember it's not based on a conditional-- it's not
5:42saying, keep doing this until we're out, or keep doing this
5:46while we still have things.
5:48It just takes the entire set, and goes
5:51through every item in that set, and will
5:53do whatever is inside the loop.
5:56Now here's the cool part.
5:57We don't have to just type it in.
6:00Remember there was a range?
6:01Well just like we can set numbers with a range,
6:04we can also get our set of data from an external command.
6:09What do I mean by that?
6:10Well, there's two different ways we can do this.
6:12The one that we've talked about in the past--
6:14remember the back tick?
6:15So if we did back tick ls, and put that-- and back tick again,
6:19so ls is inside back ticks.
6:21What it's going to do inside of our bash script is replace ls--
6:26anything inside the back tick--
6:27with the results of the command in there.
6:30Now you could also--
6:31just so you know, this is kind of important-- sometimes
6:33with weird characters, you have to do it another way.
6:36You could also do dollar sign and then
6:39the command in parentheses.
6:41This is the same thing as putting it in a back tick.
6:43I personally like the back tick-- for some reason
6:46it's easier for me to remember.
6:48So I'm going to say ls in the back tick.
6:50And now, instead of say "look it's
6:53a," we could say "I see in our current directory,"
6:59and it should go through every item in our ls command
7:02and print it to the screen.
7:03It should use the results of ls as our data set.
7:07Well first of all, let's type ls and see what's in here.
7:09We have all of the folders, and then the file, forloop.
7:13So let's see what happens when we execute this.
7:16It says, I see in our current directory desktop,
7:18I see in our current directory documents.
7:20I went through all of them, including our file itself
7:23because it's in our current directory.
7:25And it says, I see in our current directory,
7:27all of these things.
7:28So you can actually get your data set from any command
7:32out there.
7:33And whether you're doing iteration,
7:35or whether you're doing a range, or you just
7:37have a bunch of things on there, the for loop
7:39is extremely powerful, and an awesome way
7:42to go about doing things.
7:43Now a real world example that I might do
7:46is use a for loop to get a bunch of IP addresses.
7:49So first of all, let's do if config,
7:51see what range we're in.
7:52172.20.0.94, so the subnet mask is this--
7:58so we have the range of 172.20.0.1 through 255.
8:03Does that make sense?
8:04That's the network that we're currently in.
8:06So let's see if there's any other hosts on our network.
8:09Now we can do that--
8:10well there's a bunch of ways we could do that,
8:11but what I want to do is use a for loop to do it.
8:13So I'm going to say, for x in, and then I'm
8:17going to say the range of 1 dot dot 255.
8:22So that's the range that it's going to go through.
8:25And now, let's say, echo pinging 172.20.0.x.
8:35And then I don't want it to hang when it can't find something,
8:38so I'm going to use the timeout command-- this
8:40is just something else you can play with.
8:42I'm going to say timeout 0.5.
8:44So what this is going to do is, it's
8:46going to attempt to ping for 0.5 seconds.
8:50One ping, 172.20.0.$x and we should see it try to ping every
8:59item in this range.
9:01172.20.0.1, 172.20.0.2, all the way to 255 by just writing
9:08these couple of lines of code.
9:10Now this is just something you can
9:11play with-- you don't have to do a ping,
9:12you can do whatever you want in here.
9:13But if I wouldn't have put the timeout part here,
9:16it would have waited until it timed out on every single IP
9:19address, and I didn't want that.
9:21So the timeout command basically just
9:23runs whatever you put after it for however
9:25long you tell it to.
9:26So it's going to attempt for 0.5 seconds
9:28to send one ping packet to this address.
9:32Let's see how it works.
9:33I haven't tested this, so hopefully my memory
9:35has served me properly.
9:36So we'll say for loop--
9:38and sure enough, it's going pretty quickly.
9:40OK, it's trying to ping everything-- it's saying,
9:42I'm pinging this, and then it shows
9:43us the results of the ping command.
9:45So far, I haven't seen any ping response.
9:48Hopefully 0.5 seconds isn't too fast for the timeout command.
9:52Hopefully, when we get to something,
9:54it'll actually show us.
9:56OK, I snipped off a couple seconds there
9:58so we can see-- we're getting close to our own IP,
10:00hopefully we'll get a response.
10:01Ah!
10:02There we go, we got one.
10:03So it found our address, and we got that ping reply.
10:06So far, that's the only computer on our network,
10:08and I guess that makes sense.
10:10It's going to be the only computer on our network
10:12apart from the repo server over here.
10:14So you can let it go through the entire range
10:16and see if there are any other computers on your network.
10:19And again, this isn't the most efficient way
10:21to ping on your network, but it's a good example
10:23of what a for loop can do.
10:25It's done a lot of typing for us by creating these IP addresses
10:29and feeding it into the ping command.
10:32So conceptually, they're a little bit different
10:34because, again, it's not based on conditional statements,
10:37right?
10:38We don't test to see if the loop should continue.
10:40It actually just takes a complete set of data,
10:44whether it's a sequence of numbers
10:45or unrelated set of data, like a list of files,
10:49or whatever the results from a command that you put in,
10:52and it just does a loop that many times.
10:54It goes through the entire set of data
10:56and executes, between the do and the done statements,
10:59everything that you've done.
11:00And it's amazingly powerful, and something
11:03that I use a surprising amount of time
11:06while I'm being a system administrator.
11:08So the for loop.
11:09Awesomely powerful, I urge you to play with it
11:11in the lab environment-- come up with some new scenarios for how
11:15you would use a for loop in real life.
11:17I hope this has been informative for you,
11:19and I'd like to thank you for viewing.
Team training path
Turn this skill into assignable team training
This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.
$708
seat / year