Imran: Shell scripts and basic shell scripts,
and that is fun, fun, and nothing but the fun.
That's where we're gonna actually do,
write the shell scripts,
and we're gonna run very small shell script
to get you started.
And you will have your own little shells script,
own little commands that you could run.
Some of the shell script that we could cover is
like outputting to the screen using echo command.
We could create tasks such as telling what is your ID,
your current locations, the files and directories,
and some of your system information.
Another way, we could create a script
that will create your files or directives for you.
And then we could also do output
to our file within your script.
And we could use some of the text processor through scripts
and some of those source text processes are
like cut, awk, grep, sort, uniq.
All those different type of commands that we run
on a daily basis, we could automate them to see
or to get our desire result.
So I'm gonna get into my Linux machine.
(keyboard keys clacking)
I'm gonna find out the IP address of my machine,
which is 192.168.56.101.
And I'm gonna log in through a PuTTY session,
which I already have saved.
I could also type it in.
(keyboard key clacking)
Once I'm in, I could just simply log in,
(keyboard key clacking)
and I could minimize this
and I could focus on my PuTTY session.
Okay, in PuTTY session,
the first thing we're gonna do is create a directory
in our home directory.
So are we in our home directory?
Let's check, pwd.
It tells us we are in our home directory.
So I'm gonna create a directory within my home directory,
and I'm gonna call it myscripts.
And then I'm gonna go into myscripts,
but first let's verify if it created.
Yes, it did.
And then I'm gonna go into my script called myscripts.
What's inside? Of course, there's nothing inside.
I haven't created any file.
So now I want to create a first script.
The first script is to create is
whether you could touch a file and then VI it,
sut since we have to write a script, we always started VI.
And I wanted to do, I wanted to specify the name.
That would be output,
output-screen, just that,
or any name that you decide to pick.
When you hit Enter, it will bring you to the VI mode.
You hit I to insert.
And if it is a script, you always start
with #!/bin/bash.
This is defining that you gonna run the script
under Bash shell environment.
Now what do we wanted to do is we wanted
to display something on the screen.
So to display something on the screen,
you could run a command echo
and then you could type Hello World.
And once you are done, you could just simply escape
and Writequit out of this file,
and the file is created.
You could verify that it has a content that you just added
by doing cat output,
and it tells you that it does have all those contents
that you put it inside of this file.
Now can I run this file
by doing ./output-screen?
No, I can because it's not even telling me
that there is anything that exists like that,
and it's telling me it's permission denied, why?
Because we do not have
a executable permissions
to our output-screen file,
so we have to give it executable permission
to all execute output-screen,
and now do ls -ltr,
and you will see it also has changed the color,
and it has given executable right to everyone.
Now if you run this command, you're gonna see...
Always run the command.
If you are already inside of the directory,
you could run the command by ./output-screen.
When you run it, it gives you Hello World.
Hello World, this is your first script.
And if you have this, if you have created this script,
I'm telling you, you are already at the initial stage
of being a programmer.
So pat yourself in the back,
and you should be proud that you could write a script
and you could execute it and it works perfectly.
So this is one of the scripts that we did.
Oh, by the way, we could also run the script
from absolute path,
which is /home/iafzal/myscripts/output-screen.
Now in this case, we don't need ./
We could just run the script from its absolute path,
and it will give us the same output.
Now I also have some of the scripts that is saved
or written in my Wordpad.
I'm gonna bring that up, and I'm gonna review them
to tell you how we could list the command
so I could just simply copy and paste.
So here's another script that will run commands for me.
So I'm gonna do vi run-commands.
It's just a name that I'm picking for a file.
Once I do that, I hit I, insert.
I go here, copy this,
and I'm gonna paste that here.
So what it does, the first one as you should already know,
defines which shell you are in.
You are in bin/bash,
and that's where the script is gonna run.
The next one tells you the description of this script.
This defines small tasks.
So I'm gonna run small tasks is just a comment.
If I wanted to write another comment is I could do,
This script is written
by Imran Afzal,
or I could also say,
Date that was written
in 2018.
First thing that this script is doing is doing whoami.
whoami is a command that tells you who you are.
Then it's running echo.
What echo is doing at this point is only gonna give a break
inside of your script,
which it's gonna give you a space.
When you run this command,
you're gonna see the output has a space.
Then the next command is gonna run is pwd.
Next command is gonna run hostname.
Next command is gonna run is ls -ltr.
And now at the end, let's run the script. Save it.
Sorry, run command.
Oh, what? I cannot run it, why?
Again, I do not have executable permissions.
So I'm gonna assign executable permission.
I'm gonna verify it. Yes, now I have it.
Run command. There you go.
Remember the first one was whoami.
It tells me my username, which directory I'm in,
this is the directory, and hostname.
This is my hostname, and ls -ltr,
the number of files I have inside.
And the reason I put an echo in the middle,
so I could have these spaces, the empty lines,
so it will be easier for me to read it.
So that's about getting the task done
on our shell scripting, using our shell scripting.
Another way, I'm gonna cover one more script,
is to define variables.
When you define variables,
it will run those variables
to whatever the values I have defined.
So I'm gonna create another script called
vi variable-command.
Again, it is just the name of the file that I'm picking.
I'm gonna go into insert mode,
and I'm gonna copy this,
and I'm gonna paste it inside.
Okay, let's look at it, what this script is doing.
The first line of the script is basically,
again, defining the shell.
Second one is telling me what the script does.
Now this one is telling me a is for Imran, b is for Afzal,
c is for Linux class.
Now it will echo as My first name is $a.
So what is $a? Right here, variable Imran.
Then echo My surname is $b.
What is $b?
Right here is define variable Afzal.
And the third one is My surname is c.
Sorry, I should change my surname to
My class is $c.
And what is that? Linux class.
So now let's run this command, save it.
And again, don't forget to change
the permissions to executable.
(keyboard keys clacking)
And let's run variable-command.
It says, "My first name is Imran.
My surname is Afzal," and, "My class is."
It didn't tell me the class, why?
Because let's say it says Line 6: 'class
this, command not found.
Hmm, what's inside of this?
Let's look at the file, why it didn't like it.
My class is Linux class.
It should say that, but if it is not giving...
Let's try to get rid of these quotes
and then run it.
(keyboard keys clacking)
My class is, still didn't run. Let's try again.
C, my class is, because since I have a space,
it's not liking it.
Let's see why.
My class is $c.
(keyboard keys clacking)
Let's try a single quote.
(keyboard keys clacking)
And let's see if it works.
Yep, there you go. It works.
So this double quote doesn't work. Single quote does.
You might have to troubleshoot a little bit
while you are creating your script.
So I'm glad I have this problem,
so this way you will know exactly
when your script doesn't run, how you could troubleshoot.
You could tell by the error
and you could tell by the line
that it would tell you exactly, "Your script did not run
because it has an error in line 6."
And the line 6 was
1, 2, 3, 4, 5, 6.
So that's how the shell scripting works,
and I really hope by now you have created three scripts.
And I really encourage you to go online,
find shell basic scripts that you could use
and run in your environment and see how it works.