Instructor: 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, B is selected, do that, C is selected, do this.
So these are the different options
and what exactly A or B or C is gonna do.
Well, it depends on what the variable you assign to them.
For example, if you assign A 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 gonna 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 uses logged in, and D, check system uptime.
Now, this is all just a display message
that's gonna 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 gonna say, "Choices,"
which is this variable, choices,
and in the container, we put in all these choices,
and what these choices gonna do, A,
it's gonna run the command, date, B,
it's gonna run the command, LS,
C is gonna run the command list of users who are logged
in, and D, it's gonna 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 gonna go into our Linux machine,
which I have already logged in.
I'm logged in as myself, and which directory I'm in,
I'm in my home directory.
I'll go into my Scripts directory.
I believe, "My Scripts," cd myscripts,
and in that directory, I have already created some
of the scripts that we learned in the previous lessons.
So for this, I'm gonna create a new script.
I'm gonna VI and I'm gonna name the file "case-script."
You could name it anything you want.
Once you are in VI mode, hit 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, is just the 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 want you want it to do.
Second thing it's doing, by the way,
empty lines in the script doesn't really matter.
You could leave them empty or you
could just put a hash sign.
Next thing, echo.
Again, echo is just gonna throw an empty line on the screen,
and as we went over the script,
this is exactly what it's gonna do,
and we are gonna save this file.
Do LS - ITR.
This is still a file, it's not a script
'cause 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 "case-script."
Verify it has all the permission. Yes it does.
Now, we're gonna run the script
by ./ and type case-script.
Once you're gonna run it,
you'll see it's gonna 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,
it's nothing but exactly the display
of all those texts that I have typed after echo,
which you see right here, it's everything after echo.
It's nothing but an output on the screen.
So now, if I select A and run it,
it exits out the script and it gives me the date.
It will run 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 LS.
Run it again, and this time, I'm gonna pick D,
check system uptime, and the command to check system uptime
is just simply uptime, and this script will run
the uptime command and will output on the screen.
So this is 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 gonna say, "Echo, invalid choice, so bye."
So let's try to run it and this time, I'm gonna pick F.
Of course, F is not one of the listing.
So once I do that, it'll say, "Invalid Choice Bye."
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.