In this lecture we are going to create a script which can take some parameters so create a file called
file check.
As a start with siobhán on the first line
and on the next line we can write what we are going to build.
So write command write script which will work with parameters
and it will be Desch F or dash file which will provide info about number of lines vorwärts and characters
of some file
of lines vorwärts and characters
and we can also use Sparham meter dash age or dash dash help
and these will provide each message so one time aura.
The goal is to build our script with some barometers parameter or parameter file will take a value of
a file pop and it will print a number of lines voids and characters in that file.
Parameter age or a parameter or help will print usage of message.
That means message how to use our script.
So if you want to check what parameters were added We basically need to loop through all arguments so
let's do it.
We will use a while loop.
So while a number of arguments are represented as a sign hash is greater than zero.
Then perform commands between do and keyboards.
Now we should check what is the first argument and for that reason we can use case statement.
So case dollar sign or one
in.
So if the first argument is that age or degradation of legal pre-NDA usage message
so this will override echo usage.
Dollar Signs 0 which is the screen name and then the possibilities user has Desch H or does it help
Desch efile or dash dash file followed by file pop
and we can also exit discretely here as user.
Don't know how to use the script that
if the first arable land is Desch f or a dash dash file we should store a given file apart into variable
file so I the lot of to into viable file
but why we are using here go are assigned to because we are expecting to get filed back right after
a user specified dish or Dish dish file but what happens if after dish or Dish dish file is not efile
path we need to check if a file really exists.
So we'll see.
Using filed this update F and if that file doesn't exist we will print the file not exit's
so eco file
does not exits and will exit the script with exit status to
now if we have existing file we need to find out how many lines of words and characters in the file
has.
So for all lines we will use command WC vate barometer our So lines for us.
Cut the file and output of that cut command will give into standard input of command W see
four of our ads will use command debri will see with meter W
and for correctors
it will be the same bandwidth parameter and
but because we are assigning to a variable output of command we need to do command substitution.
And I will do that.
For example using batiks.
Now we can print to user information about a number of lines of words and characters.
So start with printing file name
file Galarza on file.
Then we can print number of lines
so it got lines and this is two dollar sign lines.
Then we can print number of boards so a number of words is represented by variable Lords.
And then we can print number of characters which is represented by the characters
we.
And this case section using doubles and column
and for a case that user will type something different.
We are using asterisk which is a wildcard representation of any number of characters.
And here we can be lazy and just use each line read Exit command and close the section using double
semicolon.
Now we can close the case statement all suck.
But if you are looking carefully something is not right.
We are at the beginning asking if number of arguments is greater than zero.
But number of arguments is still the same and not changing.
It would be nice to decrease the number of arguments after reports as arguments in case section and
to our surprise we have such an option and it is using keyword shift.
Every Varietes shift we are drawing ever a first argument so that means if we for example have five
arguments and then brightened the first Tuvan is throw away and we have only four arguments so far.
Desch F and dash dash.
File S..
We need to throw away that argument which represents parameter
throw a
parameters argument and also we need to throw away
debt at a woman which is your presence file but
that parameter is the value.
So if user specified only Desch f followed by some file pop we now don't have any argument.
And while we'll finish with this loop Eevee didn't have in the Help section exit commom We also would
need to use command shift which will throw every argument which is represented by parameter Desch age
or dash dash help.
And in this section is not needed as help parameter is not followed by any value as it was in the file
parameter.
So one time more for the case user has specified.
Or does this file followed by file path.
We need to throw away two arguments as we don't want to come across those arguments again as Dave already
puts us and forecast user specified Desch age or Degesch help we need to fro.
Only one argument as is debt argument was already processed and we don't want to come across with it
again.
So now we can close the script ground execute permissions
and run discrete
but firstly let's see what we can read in the current directory.
You'll see us so I will try to check that employee file from previous lecture and can see that I did
it wrongly as I didn't specify file Paromita.
So I have received the message.
If I use parameter file with employee file I received the file stat..
Great.
I see 8 lines for divorce and 199 characters.
Great.
If I change file to some not existing I receive file doesn't exist.
Great.
Now I will try to change parameters Dish dish file to Desch F and I will also art Dish dish help.
So I am using two buy meat that is either
and great.
I can see firer stat..
And also I can see using message because I have also specified help barometer.
So at the beginning of our script letter asking if number of arguments is greater than zero and it was
we have three arguments then we went into K.S. and started to ask what is the first argument.
First argument was Desch have.
So we have prepared for our statistics for a second argument which has employee file.
Then we have used shift 2 times which means that we get rid of Desch F and also we get rid of employee
file.
Now we have only one argument and it is Desch doesn't help.
The script goes again from the beginning and asking Is number of arguments greater than zero.
Yes it is.
We have one argument that doesn't help.
So we then go to the K section where we are asking for the first argument which is now help.
And in that case we are printing usage message and exiting with Aristotle's one.
If it didn't use exit command here we still have the shift to come on here which would throw away the
first argument.
In our case Desch didn't help.
And then the script would continue to the beginning of viable where we are asking is the number of arguments
greater than zero.
No it isn't.
We have zero arguments now.
So the script would continue after dummy keyboard of vital.
Now we can check some bigger file for example passport file and it works nice.
So in this way you can create your own script with numerals but ammeters.