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 scripting commands that you need to use for that is read which
will wait for your input and the output would be echoes or whatever that you input output to the screen
using the echo come in.
So let me lock into the machine and show you how this could be done before I confuse you more.
So have my machine open right here and I am in my home.
So my scripts directory.
I am going to create a script call and put script just like that.
And go at it will define the variable.
Next thing is you know you put the there.
I'm not going to just wait time and date my description and so on anyway so 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 put in like go
hello
my name
having a bad time typing.
OK.
Hello my name is Amaran soul.
And as you know it will just simply display that line on the screen.
Now what do you want to do next.
Then you want to do re.
No sorry.
Put Echo just for a empty line and then go 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 in Unix Linux that will wait for the user input.
Now you have to define a variable to read.
So any variable anything that use it types will be automatically added to the variable.
And what is that variable.
Well you have to specify or define value of let's say a pick any variable lets you pick names so we'll
say you know what.
When you put in the name put in.
Put that input into a container or into a variable and variable name is actually name 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 the user provide that information then simply put in an empty line and then do it go Hello.
And now you put a dollar name container which were the variable you have defined here.
You hit enter and go and that's pretty much it.
Let's give it proper permissions.
Missions are doorless run it.
OK you see the first line on the screen is.
Hello my name is Emraan 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 you input.
So let's say you want to type something with 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 you 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 comment.
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'm wanting to define variable.
So I'll say the variable it's a equal hostname and you know a host name is actual command in Linux that
will give you the hostname of your system.
So I wanted to say hello my Sir we're name is dollar a.
And what is that dollar a
dollar a is a 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 want change to read it to just a different container.
I'll be so just so clarify that don't you.
Do not focus on the container name contain a name could be anything.
So you're 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.
B
and bye.
OK so now what we are doing we're saying is that when you run the script tell the user.
Hello my server name is dollar.
And what is a dollar a dollar is a hostname.
And it was printed here.
And what is your name.
And you will put in your name and it will pop back to your name.
Now before I run it I just want to tell you one thing.
There will be error in the script.
Why.
Because when you define a command word 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 echo command then you have to have
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.
It actually took the exact to name that I type 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 Imraan soul and is going to say hello Imraan that's OK.
So this part from right here to here works.
This part from here to here works but I wanted to have my hostname here.
And what is my host name my host name is my first Linux VM.
I want to have that display in here.
So going back to what I was saying is if you are defining a variable and you want to run that variable
at Go then you have to put the little ticks that is right below the escape character or scaped key and
also below that that little curly thing.
So now let's run it and see if it works.
Yes it did.
You see right here.
Hello my name is.
This is the host name.
What is your name.
OK Emraan Axel
Hello Imraan.
AB-SOUL see that's how the input and output script works.
Try it 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.
It put Acco just empty line and then you do again and go What is your profession or your occupation
what you do.
And then you have to read here again read it see and then
then you do echo again one more echo and now put in.
Do you like
no let's put it.
What is your Fav for it show.
And now this time read a we have used b c now used D.
Ok that's it.
Now what do we have to do is.
First thing is to say as a go it will.
Hello my name.
Next thing I want to do is it should echo
your
user profession which is whatever the profession see is OSS.
Next one
dollar D.
Rich's favorite show 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 den I have
put all these question different containers and I am outputting those containers one by one by saying
hello Imraan.
So Atco your profession whichever is awesome and then your show whatever the show is is a good show.
Let's see if we could run it successfully and put.
OK.
The first name of the first question comes up.
What is your name.
Emraan laughs.
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 does your profession say.
Engineer.
What is your favorite show.
Sign fell and now I hit enter because I have answered all three questions and it will say hello Imraan.
So 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 its Windows or even if its Linux.
Now you know that Benue going through the wizard and 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 where ever your variable is defined.
So thats just a simple example of that.
So its like your little script Vizard that will take all the information and it will send it out on
the screen.
You could also take that information and put in your database send it out in email 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 word add fun things to add.
There are so much you could do with it and if you wanted to share your new create a script be my guess
send me an email and I hope your script runs without any error.
If you get through any problems.
Not to worry about it.
That's how we learn.