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 it, 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 that things are the 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 output to the screen using the echo, come in.
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.
I have of my scripts directory.
I am going to create a script called Input.
Script just like that, and I'm going to go ahead and hit enter will define the variable next thing
as you know, you put the author, I'm not going to just wait time and date my description and so on
anyway.
So you you 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 you put in like.
Echo.
Hello.
My name.
Oh, come on, I'm having a bad time typing.
OK, so hello, my name is Imran Eversole and as you know, it will just simply display that line on
the screen.
Now, what are we going to do next then we want to do?
Raino, sorry, Polacco, just for my line and then Echo, what is your name?
So now we are asking user for user input and the way the script is going to wait for the user input
is by simply the command read.
The read is actual command and 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 a pick any variable.
Let's say you and I pick a name.
So we'll say, you know what, when user put in the name, put in put that input into a container,
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 they use it, provides that information, then simply put in an empty line and then do it.
Hello, and now you put dollar name.
Container.
Whatever the variable you have defined here, you hit, enter and echo, and that's pretty much it.
Let's give it proper permissions.
Public submissions are there.
Let's run it.
OK, you see, the first line on the screen is, hello, my name is Imran Absol 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 going to say, hello, Jerry Seinfeld.
That's how the input and output script works.
Of course, it gets a 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 to read command.
Now, let's edit the same script, add a few more things in the script and make make it more professional.
Let's see the input script and.
Now, I wanted to define variable, so I'll say the variable, let's say a equal hostname and you know,
hostname is actual command and lennix that will give you the hostname of your system.
So I wanted to say hello, my.
So her name is Darla A and what does that Darla A?
The Dollar A is.
The a the container right here and that container is what hostname that it will show you right here
and then you'll see what is your name.
And now just for giggles, I wanted to change to read it 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 user who's going to provide the name will be put into the container called B..
Now Echo.
Hello, be.
And by OK, so now what we are doing, we are saying is that when you run the script, tell the user,
hello, my server name is.
Dollar A and what is Dollar A Dollar is the host name, and it will be printed here.
And what is your name?
And you will put in your name and it will put back to your name.
Now, before I run it, I just want to tell you one thing.
There will be errors in the script.
Why?
Because when you define a command with a variable, you could run that variable.
If you only simply typed already, then it will run it.
But if you try to run that variable using like an 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 anyway, 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 Hostname.
Well, it actually took the exact name that I typed.
It didn't think that it's a command and it now says, what is your name?
So now I'm going to put a Emran Absol and it's going to say hello, Emran.
OK, 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 display 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 escaped character or escape key and also below that that little curly thing.
So now let's run it and let's see if it works.
Oh, yes, it did.
You see right here, hello, my surname is this is the host name.
What is your name?
OK.
Amron Absol.
Hello, Amron Absol.
See, that's how the input and output script works, tried out with different things, play around with
it and ask questions 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 a code just to empty line and then you do again echo what is your profession or your occupation,
what do you do?
And then you have to read here again.
Read, see.
And then.
Then you do echo again.
One more echo and now put in.
Do you like?
Let's put it.
What is your favorite show?
And now this time read.
A, we have USB, we have you see now use D.
OK, that's now what do we have to do is first thing is have to say is go it.
Well, hello my name.
Next thing I want to do is it should echo.
You're.
Profession, which is whatever the profession sea is, or some.
OK, next one.
Dollar D, which is Fabrício is a good show.
You see how I did it first, I asked three questions, one.
To.
And three, once I have all those three questions asked.
Then I have put all these questions in different containers and I am outputting, putting those containers
one by one by saying hello.
So echo your profession, whichever 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 and put OK.
The first name for the first question comes up.
What is your name?
Imron Afsal.
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?
That's a.
Engineer, OK, what is your favorite show?
Sign fell and now and I hit enter because I have answered all the three questions and it will say Hello
Emran epso, 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 his Linux.
Now you know that when you going through the Wizard and it ask 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 the 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 wanted to share that your new creative script, be my guest.
Send me an email and I hope your script runs without any error.
If if you get through any problems not to worry about it.
That's how we learn.