Shell scripting THIRD TUTORIAL -FOR BEGINNERS -READ USER INPUT


Hello guys welcome to the next Blog on shell scripting tutorial for beginners in this Blog I'm going to say how you can read inputs from your terminal in your script so to get input from the keyboard you use read command the read command takes input from the keyboard and assigns it to a variable so let's see how we can use it so for example I want to allow the user to enter some names so I will just write for example first of all echo and then I will just print some text for example enter names or name right and then I want to allow the user to enter the name so for that I can use a command called read and then whatever variable name I right after this read command your input will be saved in this variable okay so for example I write the variable name name then your input will be saved inside this named variable now I want to print this variable so I can just write echo and then I can just write entered name and then the variable so use dollar symbol and the variable name as we have learned in the last blog so let's run the script and let's see what happens so it asks me for entering the name and I'll enter some name for example Max and then press ENTER and then it says entered name is max which is totally fine right so whenever you want to take an input from the terminal use read command now for example you want to enter multiple variables in the same read command you can just do that also so for example I want a user to enter multiple name so I will just edit this text to enter names and then instead of using one variable I can use for example name one and then space name to let me just correct name two and then space name three and so on right so whenever you want the user to input multiple variables just write the variable and then separate it by the space another variable separated by the space and then another variable and to print these names you can just print it like you do normally for example names and then first name is name one and then comma for example and then once again name - and then comma and then print them variable name three okay so let's save the code and let's run the script once again and then enter the name so for entering the name for example is right max told press enter here just enter the second name for example Tom and John I entered three names here right and then print enter and then it prints names and then whatever names I have entered here right now for example if you want to enter the input on the same line as you printed this echo string on then how you can do this so for example I am going to run this example once again and you can see it prints the string using echo and then I can see the prompt on the next line now sometimes I want to do it on the same line as this string right so how I can do this so let me remove everything here and let's say I want to allow the user to enter his username right so I can just write read and there is a flag called - P or P flag which allows us to enter the you know input on the same prompt or line right so for example I will just write use a name here and then I can take the user input for example user VAR for user variable right so this will allow me to enter the input on the same line right and to echo this I can just write echo and then whatever the user name is so for example I will just write the same text here and then the variable name which is user var okay and let me run the script once again and then it asks me the user name for example my user then press ENTER and it prints their user name now sometimes you also want to make this input silent silent means for example you want to allow the user to enter his password and the password you can not show for example right so you don't want to show what the user is typing then you can use a flag called ss4 flag right so let me just show you what I mean so for with this read you just add a s flag and then a PFLAG and then for example this is a password ok so just write password and then just change the variable name for example pass variable and then we will just print our password ok so if you want you you know skip this you can skip this line because you don't want to show whatever password you have entered but this input will be silent and let me show you what I mean by silent is so let me run the script and I will just enter the user name for example my user and then it asked me for the password I enter the password one two three four five but it doesn't show on this terminal but it still it's stored in this variable right so whenever you press ENTER you can see the password using this line of echo command the password is shown here which is one two three four five which I have printed but in here I also can see that this line is printed on the same line as the password line so I fought to solve this problem I can just write echo here with no string right and let me run the script once again so username my user and password once again one two three four five but it doesn't show on the terminal and when I press ENTER it shows everything so username and password like this now for example you want to allow the user to enter multiple inputs and you want to save those input inside an array so you can do that also and for that you use a flag called minus a so let me just you move everything here and I will just say echo and then I will just say enter names for example inter names and then I can just write read and then minus a which tells this script to read an array right so whatever variable name you give here for example names it will be like an array okay so to print those names I can just write echo once again so just write echo and then for example names and then whatever names you have entered so I can extract them like an array so to extend extract an array you can just write the variable name in the index of the array so for example at 0th index there will be a first name but to print this variable you just need to surround it by a curly bracket so just give a curly bracket like this and you can you know extract all the index like this so next will be index 1 and index 2 and index 3 and so on right for example for now I just want to expect what first two elements of an array right so let me just run the script once again a name once again for example Tom and Max and then press ENTER now it shows us the names so you can extract those inputs as an array also using this - a flag now I just want to show you one last trick here so for example I just wanted the user to enter name normally and I will just remove these inputs like this and for example I just write read here with no variable here after read command so what happens when you write read command and no variable after this so what happens is whenever you write read and without any variable after read c'mon the input goes into a built-in variable called reply so if you just write here dollar and then reply then you will be able to see your input even without your variable name so the default variable in which your input goes is this reply variable right so let me clear the terminals here and now let me run the script again so run the script and for example I write some named here once again max and then press ENTER and it prints the name whatever I have entered so by default your input goes in this built-in variable called reply so in this page you can read variables in your script using terminal I hope you enjoyed this Article/BLOG


Post a Comment

Previous Post Next Post

Recent in Technology News