How to get user input in Bash shell script?

Getting Runtime Input values with “read” command in Bash shell scripting:

We can create a shell script by using the static and dynamic method. As we all know in the static method it will not take the runtime input values, every time you run the static script it displays the same output. dynamic scripting is different from static as it takes the runtime input values.😊

To get the run time inputs:

To get the user inputs we use the inbuilt shell command “read”, what it actuallky does is it takes the input values and save it inside the variable.

The syntax for read command:

#read  <variable name>

Let us look at a simple example script

#vi  /myscript.sh

#!/bin/bash

    #Ask the user for their name

     echo  Hi, MAy I know who is this?

     read myvar

     echo "Welcome to Vasanth blog  $myvar

     :wq

 

Line 1: Defining the Interpreter

Line2: Comment field

Line3: Display a message asking the user for input

Line4: To get the input from the user and save it in the variable "myvar"

Line5: Display another message to confirm read command executed successfully
“read” command with options:

We can use the “read” commmad with a variety of  command line options . There are two commonly used options  are p, which allows you to specify a prompt message on your console, and -s which makes the input hide  i.e will not display the  given input on the console. I will show you one example of how we can apply these two options to ask user name and password.

-p = Prompts a message

-s =To hide the inputs
#vi  /demo.sh

#!/bin/bash

#Ask the user for login details

read  -p  "Username:   "  var1

read  -ps  "Password   "  var2

echo "Thank you  $var1 your authentication is success"

:wq!

Line3&Line4:  we added the prompt with quotes so that we can have a space included with it, otherwise  the user input will straight after the; last character  of the prompt

As you can see from the above output the last line i.e the prompt message is added with the password line, To print the prompt message at the new line you can add the “echo” command without any argument as shown below,  This is a good way to get a blank line on the screen to help space things out.

#vi  /demo.sh

#!/bin/bash

#Ask the user for login details

read  -p  "Username:   "  var1

read  -ps  "Password   "  var2

echo 

echo  "Thank you  $var1 your authentication is success"

:wq

Let us see another example with more inputs,
#vi /script.sh

 #!/bin/bash 

echo "Which sports do you like?"

 read   sp1 sp2 sp3 

echo "Your first sport was:  " $sp1 

echo "Your first sport was:  " $sp2

 echo "Your first sport was:  " $sp3 

:wq!

Let as run this script as shown below

#./script.sh

The read command first it will take your input and spilt it on white space. The first input will  then be assigned  to the first variable name, the second input to the second variable name and so on it continues..👨‍💻

I hope you have enjoyed this tutorial if so Kindly subscribe and share it with your friends.
                                          🙏🙏 Thank you 🙏🙏
#############################################################
For More Videos Subscribe My Youtube Channel    https://youtu.be/A7a72pmPa5o
If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe
Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way

#############################################################################################

Learn how to create a Static and Dynamic Bash Shell Script file

In this article, we will learn how to create a shell script by using the following methods

1.Static Shell script

2)Dynamic shell script

Static shell script: In static scripting, you can execute any no.of times and it generates the same output i.e it is not taking the runtime input values while executing your script.

Dynamic Shell script: It always takes the runtime input values, based on the different sets of input values it will generate different outputs.

Let us create one basic monitoring script by using the static method

Ex:1 Create a script to monitor your server performance

Method: Static

#vi   /myscript.sh

#!/bin/bash

echo "Display the Memory usage details"

echo "***********************************"

free  -m

echo "Display the Server Load average and run time details"

echo "************************************"

uptime

echo "Display the  Cpu activity and RAM Details"

echo "**************************************"

cat  /proc/meminfo

echo "***************************************"

:wq!

Now Add the Execute permission

#chmod u+x   /myscript.sh

Run the script as follows

#./myscript.sh  or sh  /myscript.sh

Once you execute the script the interpreter reads each and every line from the file and displays the output on the console.

Sample Output:👇🏿👇🏿

The main advantage of using the shell programming is automatically it executes all your larger tasks, you can combine multiple commands in a single file that are required to monitor your server performance and by adding the execute permission you can run the script file as a program.

Ex:2  Display the Total Number of Lines in a file

Method: Static

when I run a script it should display the total number of lines in a file. As all we know in Linux we have many commands to get this from the command line arguments, the same task if I put it inside the script file how it works🤗

let us create the  script file as follows

#vi  /demo.sh

#!/bin/bash

echo "Display the Total Number of Lines in a file"

cat  /var/log/messages    |wc  -l

echo "End Of Script"

:wq!

Sample Output:👇🏿👇🏿

As you can see from the above output the file has a total number of 68 lines.

Ex:3  Create a script that tells the username and prints the running process

#vi  /test.sh

#!/bin/bash

echo   "Hello,$USER

echo  "Hey, I am " $USER " and I will show you the current running process details"

echo "Display the Running processes details"

ps

:wq!

Sample Output:👇🏿👇🏿

As you can see from the above output the ps displays the running processes details from your current terminal

Note: To display all the running processes from all the terminal you must have to use the option -aux with the ps command.

Ex:4 Create a script to get the runtime input values from the keyboard(Dynamic Method)

In shells scripting, we use the command “read” to get the inputs from the keyboard, the read command  what this does is wait for the user to type something followed by the enter key it  stores that given input to the variable

Let me create a simple script to get the inputs from  by using the read command

#vi  /dynamic.sh

#!/bin/bash

echo "Hi there, What is your First Name?"

read var1

echo "Welcome Mr/Mrs. $var1, Would you like to tell us the last name?"

read var2

echo "Thanks, Mr/Mrs. $a $b for telling us your name"

echo "*********************************"

echo "Mr/Mrs, $b its time to say goodbye"

:wq!

Sample Output:👇🏿👇🏿

Ex:5 Get the inputs from the keyboard

#vi  /script.sh

#!/bin/bash

echo "Please type your name"

read  data

echo "Hi $data, Let us be friends"

:wq!

As you can see from the above output, read command takes the input which is assigned from the keyboard and then it stored the value inside the variable “data”, the last line prints the value we assigned ..  next time again run the same script, give another input you will get different output this is why it is called as the dynamic scripting(i.e It always takes the runtime input values)

In our next article, I will share you more programs by using the “read command”.

Your Valuable “thoughts” in comments is highly appreciated, please do Like and share us to spread.

If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way

For More videos Subscribe My Youtube Channel 👉🏻https://www.youtube.com/channel/UC6bR8In-jj9-klVlZQ6YH_A

 

Follow me at twitter @linuxvasanth