Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Conditionals: If Statements & Tests" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that control flows can be used in Bash scripts, and adds that if statements are syntax sugar for tests in bash. In other words, every if statement written in a Bash file is a disguised test.

Preview
Close

Transcript from the "Conditionals: If Statements & Tests" Lesson

[00:00:00]
>> So a conditional is another fancy way of saying an if statement, right? So if this is true, do this, if this is false, then do this, right? So let's go back and edit our vi file again, or our gen_files again. So the first thing I want to do, I want to make sure that the user gave an argument, if they didn't give an argument then I wanted to error out because then we don't have enough information to run a program.

[00:00:29]
So what we're gonna do under this, right here, the first before we read destination for the first time, we're gonna do if then square bracket -z $DESTINATION like that, and square bracket. Then you have to put a ; then and then we're gonna say echo No path provided, Defaulting to ~/temp.

[00:01:15]
And we're going to say DESTINATION=temp. And then you say, this is the funniest thing to me about bash, when you're finishing an if statement, you say fi, which is if backwards, right? And it's like that for all of them. Note the way that you finish case statements is with E-S-A-C, right?

[00:01:39]
So you just reverse it. I don't know why they did that, if they thought they were clever or something like that, but that's the way it is. So let's break this down for just a second because this should work as is. So, inside of this if statement, there's a bunch of different flags you can provide to this.

[00:01:56]
The -z flag is this string is empty. So if DESTINATION is an empty string, then it's gonna say no path provided, defaulting to ~/temp and it sets DESTINATION to temp, right? And then it finishes. Does that make sense? This is kind of a weird, the way this works, so if you put square brackets here, what it's actually doing is it's rearranging what you put in there into a test statement.

[00:02:26]
So let me show you what I mean. What this is actually running underneath the hood is test -z and then I'll just put like an empty string there. And then I can say echo $?. And this is either returning 0 or 1, so it's actually invoking a command and then it's checking the status code of it.

[00:02:48]
So if I come in here and say lol and then I echo that again, you can see that this actually returns 1 now because it's not true, right? So test -z lol, that is a real statement. It's not an empty string, so this is going to return false, which is 1.

[00:03:05]
Now the reason why that's useful for you to know is if I say test --help, or actually you have to do man test rather, this will give you everything that you need to know about these expressions. So you can test equals, you can test empty strings, you can test if something is empty or not empty.

[00:03:23]
You can see if they're equal or not equal, there's a bunch of stuff and it's all using test. So the way that you can remember all these different things is all you have to do is man test, okay? So if we go back to our file here, that's what these square brackets is doing is bash, underneath the hood, is rearranging this to be a test statement.

[00:03:48]
So this is useful for me to know because then I can say test -samples -z and I can test things before I put them into my if statements. Does that make sense? It's just some, you would call syntax sugar, right? This is syntax sugar for testings. And that's what those square brackets with the semicolon at the end means.

[00:04:17]
Okay, cool. So let's test it. I mean, let's make sure that I'm not just making stuff up, high probability that I do things like that. So if I say gen_files. So it says no path provided here, default into ~/temp, and if I go look inside of my temp directory, you can see here that I have all the lol files that I was talking about.

[00:04:41]
So just to show you a couple more of these cuz you probably will want to know them. You can say test 15 -= 15. Probably the way to do this is test 15 =, yeah. And then we'll do echo $?, right? So, 15 is equal to 15, right?

[00:05:10]
So that returns 0 which means that that is a true statement And you can see there, that's one. I guess another way to do this is && echo is true, which this won't run because it's not true but if I come back in here and say 15, it will echo is true because it is right, that make sense?

[00:05:39]
Then, so this is how you do numbers with this equal. But if I want to do strings, you can use an equal sign. So Brian = Brian, that's true. But if you say Brian = Mark, not true. And a couple more of these, there is greater than so if I do 15 -gt 10, is 15 greater than 10?

[00:06:08]
Yep. As you might imagine, lt is less than. There's also ge which I think is greater than or equal to, right? As is there le. Another one that's really useful that I end up using is -e. Which tests if a file exists. So if I put like, output.txt, which I'm pretty sure exists, right?

[00:06:37]
So that file exists so we can test to see, does that file exist that's often useful is like do I need to create this file to check if something's in it. Whereas if I put it in like made-up, that doesn't exist, so it doesn't echo it's true, right?

[00:06:52]
So that's super useful. Another one that's useful too is -w. So if I put output.txt there, -w checks to see does that file exist, and can I write to it as this current user? Whereas if I check like when I wouldn't be able to write to /bin /LS, that's not going to be true because it does exist but I can't, as Ubuntu, write to that file.

[00:07:24]
So and again, just check out man tests, there's a bunch more, but I just wanted to show you some of the more common ones that you'll probably end up having to use

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now