Conditionals: Else, Else If & Case Statement
Check out a free preview of the full Complete Intro to Linux and the Command-Line course
The "Conditionals: Else, Else If & Case Statement" 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 continues exploring bash script by live coding scripts with 'else' and 'case' statements, and live coding another script that contains case statements. The tools introduced are designed to give viewers the basic tools to write more sophisticated bash scripts
Transcript from the "Conditionals: Else, Else If & Case Statement" Lesson
[00:00:00]
>> So I'm gonna make another file here inside of my bin and this is going to just be called like check or we'll just do touch. Check number and we'll do a CH plus x of check number and then we're going to vi check number and then we'll do our little hash bang at the top slash bin slash bash.
[00:00:33]
Okay and then we're going to say if. Dollar sign 1. Space is a little bit more readable is greater than 10 then. Echo greater than 10. Else if that's how you write an else if is elif, write that. We can write another test in here. Dollar sign one is less than ten then echo.
[00:01:21]
Less than 10. And then finally you can do else, echo. Equals 10. And then again, always at the end, i I like that. Okay, so now I can write quit here. And I can say, check. Number 10. All right, so check number. So if I put 10 it says, equals 10 if I put 11 it's greater than 10.
[00:01:59]
And if I put 9, it is less than 10. So that's how you do if elsif else and then you always have to finish with the ephi. Okay, let's do one more. We'll make another file here. We're going to call it smiley. So touch smiley and we're going to chmod+x smiley and then we're going to vi on smiley.
[00:02:28]
I'm going to do a case statement really quick. So we're going to say case, $1. This is like a switch statement in JavaScript. And I have to put my bin bash in there. /bin/bash, also kind of a tip here. I don't know how to do this. I'm sure there's a special way to do this with the vi.
[00:02:53]
But if I just right-click quit and then reopen again. It'll use this to interpret, okay, this is a bash file. I will give you bash highlighting now. I'm sure someone in the chat is immediately gonna say, this is how you do it with vi. That's fine. Anyway, case $1 in, and then here.
[00:03:12]
It's gonna just be a matching statement. So if the user input smile here in quotes, I'm gonna put the string that it's looking for and then you put an end parenthesis to signify this is the first case. And I'm going to say echo. Our little smiley face there and then the way that you end a particular cases to semi colons like that.
[00:03:46]
Okay, we'll do the next one here, sad echo, Sad face and then we'll do laugh. And I have more of these in my notes but you get the point, echo and okay. And in order to finish that is you do and then the last one that's good to know is like if it doesn't match any of these to put the wildcard at the end.
[00:04:21]
You say this will only get hit. If it doesn't match anything else, then you say I don't know that one yet. Okay. And then again, the way that you finish a case statement is with case backwards which is esac, esac, okay? And then we'll write- quit there and then we can run smiley with laugh and I'll put that smiley smile and smile something else, right?
[00:04:58]
And it's gonna say, oops, you need to put smiley. I don't know that one yet. What did I mess up in there? I didn't put an echo. Right there echo. There you go, then it says it don't know that one yet. But you can see that that block code wasn't being executed on the previous ones it wasn't erroring out, because they're being executed.
[00:05:37]
So the switch statements here can be get pretty fancy and I'm not gonna really get into it too much. But suffice it like you now have the tools that if you need to get more fancy with switch statements, you know how to Google that.
>> Is there a concept of Local scope with bash scripts?
[00:05:54]
>> There is. So anything that I set inside of my bash script is not going to be exported outside of that bash script, unless you explicitly say export this and then that will be exported to the global scope like the global scope. Meaning that it will survive beyond the bash script as far as like scoping within like blocks.
[00:06:17]
I don't actually know what the block scope of bash scripting is. My intuition tells me that there's no block scope, but you'd have to look it up. I don't actually know. Do I think programming with bash is a different mind set than programming with like a different programming language?
[00:06:36]
I guess, I mean, programming is programming is programming and I would loop bash in as a traditional programming language, but I wouldn't like go write like a web server in bash like it's totally possible. It's absolutely possible to be able to do that. I use bash much more as a utilitarian language like I'm trying to do a one off scripts and I wanted to do one thing, and I wanted to be done with it after that.
[00:07:05]
So in that sense, I'm probably a little less diligent about making super clean code. It's much more procedural, I think that's probably one of the biggest things about it is when I write my node code, I tend more towards a functional style. I tend to have a lot more functions and classes, and objects and things like that.
[00:07:29]
Whereas bash, it's very much reads this line, then this line, then this line, then this line, then this line. And anything that's much more complicated than that, I will probably reach for note or I'll probably reach for Python. Whereas I treat bash much more procedurally and I think that's common, it's not necessarily you have to do it that way.
[00:07:48]
So one thing I'm not gonna show you, but is possible with bash scripting is you can write functions with bash scripts. The reason why I'm not showing is like I just don't anytime that I need to write functions, I'm getting into something a bit more complicated. And I would probably reach for Python, because Python also is very good at doing the similar things to bash.
[00:08:06]
But I know plenty of people that would write that in bash. So that's probably a pretty roundabout answer of like the way that I treat bash, yes, but there's no reason that it has to be that way. It's good question.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops