Shell scripts and basic shell scripts.
And that is fun.
Fun and nothing but the fun.
That's where we're going to actually do write the shell scripts and we're going to run very small shell
script to get you started, and you will have your own little shell script on little commands that you
can 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 new files or directories for you.
And then we could also do output to a 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 or grep sort unique.
All those different type of commands that we run on a daily basis.
We could automate them to see or to get our desired result.
So I'm going to get into my Linux machine.
I'm going to find out the IP address of my machine, which is 1921 6856 101 and I'm going to log in
through a putty session, which I already have saved.
I can also type it in.
Once I'm in, I could just simply log in and I could minimize this and I could focus on my putty session
in putty session.
The first thing we're going to do is create a directory in our home directory.
So are we in our home directory?
Let's check p w d.
It tells us we are in our home directory, so I'm going to create a directory within my home directory
and I'm going to call it my scripts and then I'm going to go into my scripts.
But first, let's verify if it created.
Yes, it did.
And then I'm going to go into my script called My scripts.
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 buy it.
But since we have to write a script, we always start at the I 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 and 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 hash bang slash bin bash.
This is defining that you are going to run the script under bash shell environment.
Now what do we want it to do is we want to display something on the screen.
So to display something on the screen, you can run a command echo and then you could type Hello world.
And once you are done, you could just simply escape and write quick 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 LZ, by doing dot slash output screen?
No, I can because it's not even telling me that there is anything that exists like that and it's selling
me its permission denied.
Why?
Because we do not have executable permissions to our output dash screen file, so we have to give it
executable permission to all execute output screen and now do ls my center 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 going to see always run the command.
If you are already inside of the directory, you can run the command by dot slash and output and you
run it.
It gives you a 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.
I have all my scripts and output now.
In this case, we don't need dot slash.
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 word pad.
I'm going to bring that up and I'm going to review them to tell you how we could list the command so
I could just simply copy and paste.
So here is another script that will run commands for me.
So I'm going to do VI.
Run Commands is 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 going to paste that here.
So what it does, the first one, as you you should already know, defines which shell you are n, u,
R and Ben Bash.
And that's where the script is going to run.
The next one tells you the description of the script.
This defines small tasks.
So I'm going to run.
Small task is just a common.
If I wanted to write another comment is I could do this script is written by Imron Absolute or I could
also say date.
That was written in 2018.
First thing that the script is doing is doing.
Who am I?
Is a command that tells you who you are.
Then it's running Echo.
What Echo is doing at this point is only going to give a break inside of your script, but it's going
to give you a space.
When you run this command, you're going to see the output has a space.
Then the next command is going to run is the next command is going to run.
Hostname next command is going to run is LZ minus LTR.
And now at the end, let's run the script.
Save it so you run command.
What?
I cannot run it y again, I do not have executable permissions, so I'm going to assign executable permission.
I'm going to verify.
Yes.
Now I have it.
Run command.
There you go.
Remember the first one was Where am I?
It tells me my user name, which directory I am in the Z directory and host name.
This is my host name and S minus l t are the number of files I have inside and the reason I put an echo
in the middle so I could have these spaces as the empty line 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 going to 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 going to create another script called v I.
Variable command again, is just the name of the file that I'm picking.
I'm going to go into insert mode.
I'm going to copy this.
And I'm going to paste it inside.
Let's look at it.
What this script is doing, the first 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 Imron.
B is for Absol.
C is for Linux class.
Now it will echo as my first name is Dollar A.
So what is dollar a right here variable Imron then?
Echo My surname is dollar B what is dollar B right here?
This defined variable apso.
And the third one is my surname is C Sorry, I should change my surname to my class is dollar 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.
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 six 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 getting.
Let's try to get rid of these codes and then run it.
My class is still didn't run.
Let's try again.
See, my class is because since I have a space, it's not liking it.
Let's see why my class is dollar C.
It should have.
Let's try a single coat.
And let's see if it works.
Yep.
There you go.
It works.
So the double code doesn't work.
Single code 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 you 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 will tell you exactly your script
did not run because it has an error in line six.
And the line six was one, two, three, four, five, six.
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, bash basic scripts that you could use and run in your environment and see how it works.