Case statements scripts.
Now this lesson 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 want 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 is selected do this.
So these are two different option and 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.
What if A is selected.
Now let's look at the script
I have a script right here.
Always starts with the Shell Shell is bash.
The next is Atco 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 displayed date and time B list file and directories see
list users logged in and D.
Check this system up time.
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.
Put all the choices in a container then the case statement comes in it's going to say choices which
is the variable choices end in the container we put in all these choices and what these choices are
going to do.
A It's going to run the command.
Date B it's going to run the command LS C is going to run the command list of users who are logged in
and Dede's going to run the command up time.
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 to treat.
I'm and I'm in my home directory.
I'll go into my scripts directory
and leave my scripts CD 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 with no VI and I'm going to name the file case script.
You could name it anything you want.
Once you are in B mode hit insert high and you could just simply 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 your search of your name or whatever what you want to do.
Second thing it's doing by the way.
Empty lines in the script doesn't really matter you the empty or you could just put a hash sign next
thing.
Echo.
Echo is just going to throw in an empty line on the screen.
And as we went over the script this is exactly what's going to do.
And we're going to save this file.
That's my cell.
TR This is still a file of a script because we have not assigned to executable permissions to it.
So to assign the secure permission A-plus X to assign executable permissions to all user group and others
and the name of this group is case crep 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 text that I have typed
after echo which you see right here.
It's everything up to Acqui it's nothing but an output on the screen.
So now if I select a and run it it exit 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 will be.
Once you Dupee it lists all the files or directories inside of my current directory which is a command
that you already know which is ls.
Run it again.
And this time I'm going to pick the check system uptime and to the command to check system time is just
simply uptime and this script will run the uptime command output on the screen.
So this will what exactly the case statement does.
So the last option if you know is we have a star right here.
What it's telling me that if any other option is pect it's going to say Ecko invalid choice.
So bye.
So let's try to run it and this time I'm going to pick f of course f is not one of the listing.
So once I do that they'll say Invalid choice.
BI So that's how the case script works.
I want you to try it out.
Pick different commands that you want to run Pegge different text message that you want to associate
with those commands and try to run it.
Let's say if you wanted to pick a 4 What is your name and it should type who for it and will give you
the logon name and so on and all the different commands that you have learned.