Instructor: Input and output..
This lesson is about writing a script
where the script will wait for the user input.
So when you run the script, the script will wait
for you to type in something.
So it would take your command and then output based on
that written statement that you have given to the script.
So the way it works is the things or the scripting commands
that you need to use for that is read,
which will wait for your input and the output would be echo.
So whatever that you input, it will output to the screen
using the echo command.
So let me log into the machine and I will show you
how this could be done before I confuse you more.
So I have my machine open right here and I am in my home,
iafzal, my scripts directory.
I am going to create a script called input-script,
just like that.
And I'm gonna go ahead and hit enter.
I will define the variable.
Next thing, as you know, you put the author,
I'm not gonna just for a time, I'm just a date,
description and so on.
Anyway, so you should type everything.
I would definitely recommend you.
So again, since you don't have time,
I really want you to learn fast, so don't worry about it
at this point.
Then you put in like echo.
Hello.
Hello
my name.
Oh, come on.
(laughing)
I'm having a bad time typing.
Okay, so hello, my name is Imran Afzal and as you know
it will just simply display that line on the screen.
Now what we want it to do next,
then we want to do read, no sorry, put echo just
for a empty line and then echo, what is your name?
So now we are asking user for user input.
And the way the script is gonna wait for the user input
is by simply the command read.
The read is actual command in Unix or Linux that will wait
for the user input.
Now you have to define a variable to read.
So any variable, anything that user types
will be automatically added to the variable.
And what is that variable?
Well you have to specify or define that variable.
Let's say I'll pick any variable.
Let's say even I pick name.
So we'll say, you know what, when user put in the name,
put in, put that input into a container or into a variable
and their variable name is actually name.
I'll just, you know what in simple words name container.
So, make it easy so you don't, you are not confused
with name, name itself.
Now once a user provides that information,
then simply put an an empty line and then do echo, hello
and now you put dollar name container of whichever
the variable you have defined here.
You hit enter and echo.
And that's pretty much it.
Let's give it proper permissions.
Proper permissions are there.
Let's run it.
Okay, you see the first line on the screen is,
"Hello my name is Imran Afzal or your name."
And now it's asking you, "What is your name?"
So it's waiting.
See you didn't get the prompt back.
Why, because the script is waiting for your input.
So let's say you wanted to type something,
let's say, "Jerry Seinfeld,"
and now you hit enter
and it's gonna say, "Hello Jerry Seinfeld."
That's how the input and output script works.
Of course it gets some little bit more complicated
as we go on, but basically if you wanted to write a script
and you wanted to take the input of the user,
you have to use the read command.
Now let's edit the same script, add few more things
in the script and make it more professional.
Let's see, VR input script and now I want it
to define variable.
So I'll say the variable, let's say A equal host name
and you know host name is actual command in Linux
that will give you the host name of your system.
So I want it to say, "Hello, my server name is $A."
And what is that $A?
The $A is the A, the container right here.
And that container is what?
Host name that it will show you right here.
And then you'll say what is your name?
And now just for giggles, I wanted to change the read
to just a different container.
I'll put B.
So just, so clarify that don't you,
do not focus on the container name.
Container name could be anything.
So you are asking what is your name and the the user
who's gonna provide the name will be put
into the container called B.
Now echo.
Hello B
and bye.
Okay, so now what we are doing, we are saying is
that when you run the script, tell the user Hello,
my server name is $A
and what is $a, dollar is the host name
and it will print it here.
And what is your name?
And you'll put in your name and it will output back
to your name.
Now before I run it, I just wanna tell you one thing.
There will be error in the script.
Why, because when you define command with a variable,
you could run that variable.
If you only simply type $A, then it will run it.
But if you try to run that variable using
like a echo command, then you have to have this ticks
on the side.
What are those ticks?
Those ticks are right below your escape key.
Those are the ticks.
So you have to have those.
But anywhere I want to take it out, for now,
just so you see that what do we get
if we don't put those ticks in there?
So escape, save it.
Now let's run it.
It says, "Hello, my server name is host name."
Oh it actually took the exact name that I typed.
It didn't think that it's a command.
And now it says, "What is your name?"
So now I'm gonna put it Imran Afzal
and it's gonna say, "Hello Imran."
Okay, so this part from right here to here works.
This part from here to here works.
But I wanted to have my host name here
and what is my host name?
My host name is my first Linux VM.
I wanted to have that displayed here.
So going back to what I was saying
is if you are defining a variable
and you want to run that variable with Echo,
then you have to put the little ticks that is right below
the escape character or escape key
and also below that little curly thing.
So now let's run it and let's see if it works.
Ah yes, it did.
You see right here?
Hello, my server name is, this is the host name.
What is your name?
Okay, Imran Afzal.
Hello Imran Afzal.
See that's how the input and output script works.
Try it out with different things.
Play around with it and ask question back and forth,
back and forth.
So first you should, what you should do is as your practice,
what you should do is you could put more than one read.
How you could do it is like this.
You put echo just a empty line and then you do again echo,
what is your profession or your occupation
or what do you do?
And then you have to read here again, read C,
and then then you do echo again.
One more echo and I'll put in, do you like,
no, let's put it, what is your favorite show?
And now this time read A, we have used B, we have used C.
Now use D.
Okay, that's it.
Now what we have to do is first thing it has to say
is echo, it will, hello my name.
Next thing I want to do is it should echo your profession,
which is whatever the profession is, C, is awesome.
Okay, next one.
$D, which is favorite show is a good show.
You see how I did it?
First I asked three questions, one, two, and three.
Once I have all those three question asked
then I have put all these question in different containers
and I am outputting those containers one by one by saying,
"Hello Imran Afzal," echo, "your profession,"
whichever it is, "is awesome."
And then, "Your show," whatever the show is,
"is a good show."
Now let's see if we could run it successfully slash input.
Okay, the first name, the first question comes up,
what is your name?
Imran Afzal
See now it gave me a second question.
It didn't give me the output right away.
Now it's waiting for me to give it
all the information it wants.
What is your profession?
Let's say engineer.
Okay, what is your favorite show?
"Seinfeld".
And now and I hit enter because I have answered
all the three questions and it will say,
"Hello Imran Afzal, your profession, engineer, is awesome.
"Seinfeld is a good show."
See how it works.
Now think of this now.
When you are installing a program
or you are executing some kind of script
and or even if it's Windows or even if it's Linux,
now you know that when you going through the visit
and it asks you questions, do you want to install that?
You say yes, no, put in your organization name, put it this
you type in your name and this and that,
all that information that system gathers from you.
It actually plugs that information inside wherever
your variable is defined.
So, that's just a simple example of that.
So it's like your little script, a wizard
that will take all the information
and it will send it out on the screen.
You could also take that information
and put it in your database, send it out in his email
or whichever the way you want.
So that's how you work with input and output scripts.
Again, I will say go ahead and keep that script long
add more things to it, add fun things to it.
There's so much you could do with it.
And if you want to share that your new creative script,
you might just send me an email
and I hope your script runs without any error.
If it, if you get through any problems,
not to worry about it.
That's how we learn.