Hello everyone.
In this demo, I'm going to show you some examples of bash scripting.
Let's start with the first example, the user input.
So I'm going to start, as you can see over here.
Here are some examples.
So I'm going to start with the first example here.
The user input, the strings that is So strings go.
Here we see what is the script over here?
As we can see here, this is the shebang.
The first line and here is the comment, I think nothing new over here.
So this is says that this is the this script shows your first name.
Also.
Maybe you can type here also your first name and last name.
Perfect.
Next, as we can see over here, echo the echo command.
We're going to all in this line just we are going to type or we're going to show just this message.
Hello, may I know your first name?
Then we can see here.
This is the command read.
So this command is going the read command is going to transfer the control from the running script,
which is the script.
To the user in order.
Why?
To let the user to type the first name and the last name, then store whatever the user enters.
So we're going to store.
For example, if the user entered, the first name is John, so he's going to store.
Store or save the the name over here and this variable.
A first name.
And there is also a second message over here.
We asking the user to enter the last name and we're going to save whatever the user is going to enter
and the last name variable.
And here we're going to show a message after that.
So thank you.
Then as we can see, this is the dollar sign.
Why we're using the dollar sign before the variables, because we would like to show what the user entered.
So we're going to show the value.
We're not going to show the name first name if we're not going to put the dollar sign.
So the system is going to show first name and last name.
So the reason we put the dollar sign, because we would like to print the value of the variable.
Perfect.
So far, so good.
Now let's run this.
So control, so shift x colon X, then the bank or the exclamation mark hit Enter.
Now I'm going to run this script.
Strings.
That is hit enter.
So as you can see over here.
Hello.
May I know your first name?
Say, John.
Then hit enter will ask me the second question.
Say your last name.
You know, just I'm going to just type uppercase L hit enter.
As we can see here, this is the last message.
Thank you, John.
L So far, so good.
Perfect.
Let's move on right now to the second example.
So the second example that I would like to show you is the sum.
So sum that is H hit enter.
So as we can hear, this is the shebang line.
This is the comment.
So this script shows the sum of the three numbers.
So as we can see here, here's the parentheses because we would like to do some between three numbers
and we put so sum here is the variable then equal 20 plus 30 plus 40, and we put all of them inside
double parentheses, as you can see here, two parentheses at the beginning and at the end, two parentheses.
Then here we're going to show a message echo.
The numbers are 20 plus, 30 plus 40.
And the second or the last line here or the third line.
The last line here is the result is and as I mentioned in the previous script, we need to.
Type the dollar sign and then the name of the variable.
So this is the variable here.
So we need to do that in order to show what is the value of that variable.
Perfect.
Just run the script.
So some that search hit enter.
As we can see here, the numbers are 20 plus, 30 plus 40.
And here is the return.
So here is the sum of these three numbers.
You can do the same thing.
But as I mentioned before, since I do cat home slash John and slash dash bash says hit enter.
As I said that because I added this line over here.
This is the line and this file is the configuration file.
So I don't need to type the dot slash or the bash before the name of the script file.
Okay.
Now let's move on.
Let's move on to the third example.
So the third example here is the length.
So the length that is H So what is the purpose of this?
This is the shebang line.
So here I would like to show a message to the user.
Could you please type your name and your age?
And we have the read command.
And as I mentioned before, the read command is going to transfer the control from the running script
to the user.
If you guys remember when we showed that message and we asked the user to enter the value, So so the
so is going to lead the user to type the the name, then the age, then we're going to store whatever
the user entered in this variable in the length variable.
Then what we're going to do here, we see the message over here is the last night.
So the length of this string.
So the purpose of this script, I would like to show what is the length of the variable that the user
is going to type.
And I know it's kind new for you guys.
So here is the dollar sign, the reason of the dollar sign because we would like to get the value.
Then we have here what we have.
We have the braces, then hash length, then we close the braces.
So we it's really important that we know the structure because this is the syntax structure of the bash
scripting.
So in order to get a good outcome, we need to know the structure.
So dollar.
Then we open the parenthesis and we close it.
Then inside that hash length, let's run the script.
So length that is h So what's your name?
My name is John.
And say I'll say here, my name is John and for example, not 50, 30 years old, hit enter.
As we can see over here, this is the message.
The length of this string is 37.
So this is the length and also is going to consider the spaces.
So from here to here, if you're going to count them, they are 37.
Okay, So now let's move on.
There is a file also I would like to show you before I'm going to explain the fourth example, the if
else.
Okay.
So cat slash home slash and I save that file under the directory.
That is examples and operators hit enter.
So this is the syntax that we're going to use in the bash scripting.
So here is the samples.
We're going to use those, the syntax.
So the dash EQ, it means that equal, equal.
So it means equal if we'd like to compare between values, for example, if I would like to compare
between two values, the value one it's two and value three.
So value one is two and value two is three.
Are they equal?
Not they are not equal.
And this is the another syntax that we're going to use in the in this, the bash script.
So dash and E, it means not equal and here is dash get greater than dash G so it's greater than or
equal and dash lt is less than or dash la so is less than or equal.
Okay, now let's run the script.
So the script is f ls f dash ls h enter.
So as we can see over here.
So the purpose of this script, we would like to ask the user, so to ask the user a question, what
is the root user uid?
The Uid, so you know that every user has a Uid.
Then this is the read command.
So it's going to transfer.
As I mentioned before, is going to transfer the controller from the running machine running script
to the user in order to type.
For example, type whatever string or ID.
And here is a comment said the Uid of root user is zero and here we have the.
So as I mentioned before, we need to know the structure in order to understand any programming language
or even the bash scripting, you need to know the structure.
So for if else is a condition.
So we type if.
Then else.
Then we close the F, so F, then we close this f.
So between here, as you can see over here, this is the.
The brackets.
So between the brackets, we need to leave spaces at the beginning and the end.
And here the value is zero.
So this one is what?
Equal.
So if this value equal to this value.
So the dollar sign.
And we need also to put in between the quotation mark because we're going to compare these two values.
So I'm going to compare this value with this value.
If they are equal.
So if the first value is equal to the second value, then.
Print this message.
If this value is not equal to this value, then we're going to skip this.
We're not going to do anything.
So we're not going to print this line.
We're going to go to the else.
So if this is true, then we're going to print this one.
If this is false, this line is false, then we're going to print whatever is under the else.
So if you are not the system administrator, let's run this script.
So if else that is h hit enter.
So what is the root user uid if I hit zero, you are the system administrator.
Let me show you the content of this script.
Shebang line doesn't matter here.
So this is a question.
Perfect.
We give the user and we lead the user to enter the value we saved zero in this variable.
So the value of ID is zero.
Then it's a comma.
Here is the comment.
It doesn't matter.
So we're going to take.
So if this value is zero, it's already zero equal to this value, which here is the value here.
So the dollar sign ID, then print this one and this is what the outcome of the script.
You are the system administrator and we get this message if this one is not true.
False.
We're going to print this message.
So far, so good.
Perfect.
Now let's move on to the fifth example.
The while loop.
So for a while loop.
That is it.
So this is a loop.
If you hear about loop, it means that it's going to is like there is iteration that is going to go
over values like more than once.
Okay.
Let me explain.
What what do you mean by that?
So here is the this script prints the numbers from 1 to 10.
So this is the main objective of this script.
And this is another comment here.
This is the initial value, the initial value for the X, We assign a value to this variable.
So the variable is X, So x equal one, it means that I'm going to assign number one, the value to
this variable.
Next here is a comment.
So this is the while loop.
So the while loop here we have the while do then done.
So if the while if this line is true, then do this line.
It means you go inside and print this line.
Then if it gets notice here we have the parentheses x and c x plus plus and between double parentheses
y we need what does it mean?
That x plus plus it means that is going to increase the value.
So the initial value, it's one.
So we're going to so we're going to run the script.
So it's going to run line by line.
We'll ignore.
So this is the shebang.
Ignore this line, ignore this line.
Then we'll assign one to the X, then ignore this line.
Then we'll go to the Y line.
And we have here we have the brackets.
So the brackets here, as I said, that you have to leave a space at the beginning and end.
And then as I said before, we need to use the dollar sign in order to get the value.
We don't want to use the variable.
We don't I don't need the x Y.
I need to I need to compare X to ten.
I need to compare the value of x, which is 1 to 10.
So he said if one is less.
Then ten.
Then do this line.
So the number is and we're going to print the number.
So here is one.
One is less than ten.
Good.
True.
So the number is one.
Then we're going to increase it.
So we're going to go back again here to the while line here.
So this time.
So after we increase the number to two, two is less than ten.
Yep.
Good.
Then we're going to increase.
We're going to print two, then increase the number 2 to 3 and go up again.
And here is three less perfect print three, then increase it to four.
We're going to do it till we're going to reach what, two number ten.
So if we're going to let me run the script so we can get an idea about it.
Okay.
While loop, while dash loop that is which enter as we can see.
Three.
So cat while loop.
Ginger.
So the initial value here, we compare this 1 to 10, perfect is true.
So we're going to print this one.
So the number is one, then we increase two, two two is less perfect.
We're going to print two, then increase it to three.
We're going to do it till ten.
Once we're going to increase it to 11, the last value here, we're going to go over here and is it
11 less than ten?
No, false.
So we will jump from here to done.
We're not going to do.
This two lines anymore because we 11 is less than ten.
Perfect.
So far, so good.
Okay, let's do the sixth example.
And this example, it's the for loop.
So it's the same concept.
It's a different structure.
Both of them are loops.
The while has different syntax and therefore has different syntax.
So here is the same thing printing from 1 to 10 for loop.
So here, let's see the syntax of the for loop.
So we have for do then done.
So if let's go for here let's go to to this line for then we have the double parenthesis.
We need to put the condition.
So the difference here, if you guys notice in the while we assign the initial value before the Y.
Command.
Although I lie here for the four, we have three things we need to do it inside the double parenthesis.
We have the initial value, then semicolon, then we have the condition, then semicolon.
Then we have the increment that we're going to increase the number.
So the initial value for x is one, then semicolon.
Then we're going to compare X.
Is it equal?
Is less or equal than ten, then semicolon, then we increase.
Then here.
If it's true, if this line is true, then we print the number last.
Let's do a demo here.
We're going to run the script.
Hit enter cat for loop hit enter here.
So these three lines, we just ignored them.
This is the shebang line.
Comment.
So here is the initial value one.
X, It's one here.
The value less than or less than or equal to ten.
True.
Then we're going to increase the value and then print this message.
The number is one and we're going to do that.
We're going to print the last value.
So we're going to reach here to 11.
So here is so when are you going to be over here?
If it's here, the condition is false.
It will jump.
It's not going to look to these two commands will jump to the to the last line, to the done.
So far, so good.
Let's now move to the last example.
The last example is the array example array.
So the arrays, that is each.
So the array is like a list.
You have a list of items.
And as we can see over here, here is the name of the array list equal.
Then we have the parenthesis.
We open parentheses, just we have single parenthesis beginning and at the end then here is the items
inside this array.
So and we need to type the items or the value of each.
Uh, index.
I will explain what I mean by the index.
The index is like, uh, you can compare them like a storage.
So the first, uh, storage is zero.
This is the index or the number of that.
The first storage is zero.
This is the index zero.
This is index one and this is index two and this is index three.
So for the array we start.
The numbers when we start the index from zero.
Then we go to one.
Then we go to two, then three.
So this is the index value.
So if I will say what is the index value of the index value on list array?
The index zero, let's say the index zero of the list array is sent to us.
What the index value.
What is the value of index two?
So here is 012.
It's fedora.
And what is the index value of?
What is the value of index three?
It's upon two.
And so this is the array.
And we have the for loop.
I already explained the for loop, but there is something interesting over here.
We have the echo message and here the the dollar.
Then the value, the variable x.
It means that we would like to print the value of the variable x is.
And here is the the interesting part.
So here, why?
Why I wrote it like that way because I would like to print the items inside the array.
The list array.
So as we can see, here is the dollar sign.
So we use the dollar sign, then the braces.
So we open one press here at the beginning and then as last press at at the end, then the name of the
array, which is list.
Then between.
The sea here between the brackets, we put the value of the X.
So all of these, because we are using the dollar sign at the beginning.
We don't have to put the dollar sign here.
So the dollar sign that's outside the the, the, the braces, then the name of the array, then brackets,
then we're going to print the value of X.
Let's run this script.
So arrays enter, as we can see over here, let's say cat arrays.
So here is the value of index zero.
So this is index zero is sent to us and the value of index one is read had the value of index two is
fedora and the value of X of index three, it's up one, two.
And how did we bring that?
We use the for loop.
So this is the condition here.
Zero is zero.
Less than three.
Yes.
True.
Then we're going to print this line and increase.
Also increase the value.
So we're going to print zero here as the first value.
Then we're going to print the value of index zero and we're going to do that till we're going to reach
two for four.
Is it for less than or equal three?
No, that's false.
This line is false.
Then we're going to jump to the end.
So that's conclude our session.
Thank you very much.