Case statement scripts.
Now this lesson we will go over the statements of case where you are given options in a script to pick
from.
So it's like an interactive way of involving user, whether they wanted to select option A, B, C,
or so forth.
So in case statement, if a option is selected, do this, be selected, do that.
C you selected, do this.
So these are the different options of what exactly A or B or C is going to do.
Well, it depends on what the variable you assign to them.
For example, if you assign a to do to output date on the screen, it will output the date if A is selected.
So now let's look at the script.
I have a script right here.
Always starts with the shell.
The shell is bash.
The next is Echo, which is just printing out an empty line.
Then it's going to echo.
Please choose one of the options below.
It will print out a as in for variable equals display date and time.
B list file and directories.
C list users logged in and D check system uptime.
Now this is all just a display message that's going to show up once the script is run.
Then it will read all the choices.
It'll put all the choices in a container.
Then the case statement comes in.
It's going to say choices, which is this variable choices.
And in the container, we put it, all these choices and what these choices are going to do.
A It's going to run the command date.
B is going to run the command.
LS.
C is going to run the command list of users who are logged in and D it's going to run the command uptime.
Now, remember, all of these are actual commands inside of your Linux operating system.
When you run it individually, it will give you the date.
But this script will allow you to automate the task to pick these commands for you.
So let's copy them.
This is the whole script we have.
And I'm going to go into a Linux machine, which I have already logged in and logged in as myself and
basic directory I'm in, I'm in my home directory.
I'll go into my scripts directory.
Um, I believe my scripts seed my scripts, and in that directory I have already created some of the
scripts that we learned in the previous lessons.
So for this, I'm going to create a new script.
I'm going to VI and I'm going to name the file case script.
You can name it anything you want.
Once you are in VI mode head insert I and you could just simply right click on your on your mouse and
it will paste everything for you.
The first option is once again, it's just a shell.
It's telling you to run which shell environment.
The second one is you could type in anything.
This script is created.
By me or yourself, your name or whatever you want you want to do.
Second thing it's doing, by the way, my lines in the script doesn't really matter.
You can leave the empty or you could just put a hash sign.
Next thing.
Echo again.
Echo is just going to throw an an empty line in the screen.
And as we went over the script, this is exactly what it's going to do.
And we're going to save this file, do less minus later.
This is still a file.
It's not a script because we have not assigned executable permissions to it.
So to assign the executable permission, a plus X to assign executable permissions to all user group
and others.
And the name of the script is K Script Verify.
It has all the permission.
Yes, it does.
Now we're going to run the script by dot slash and type K script.
Once you're going to run it, you'll see it's going to give you that.
Please choose one of the options below.
So you have the list of options right here.
A To display the date and time is nothing but exactly the display of all those texts that I have typed
after Echo, which you see right here.
It's everything after.
It's nothing but an output on the screen.
So now if I select a.
And run it.
It acts it out the script and it gives me the date.
It runs the command date itself as if you type date.
It gives you the date, today's date, the time and year.
Let's run it again.
And this time we'll pick B.
Once you do B, it lists all the files or directories inside of my current directory, which is the
command that you already know, which is Ellis.
Run it again.
And this time I'm going to pick DH check system up time and took the command to check system uptime
is just simply uptime and this script will run the uptime command and output on the screen.
So this what exactly the case statement does.
So the last option, if you notice, we have a star right here.
What it's telling me that if any other option is picked, it's going to say echo invalid choice.
So by so let's try to run it and this time we're going to pick F.
Of course, F is not one of the listing.
So once I do that, it'll say invalid choice by.
So that's how the case script works.
I want you to try it out, pick different commands that you want to run, pick different text message
that you want to associate with those commands and try to run it.
Let's say if you run it, pick a for what is your name and it should type who for it and it will give
you the login name and so on and all the different commands that you have learned.