Shell Scripting | Creating Script Using User Input

bash shell scripting with user input read command

Please Read This 👇🏾


In previous post, We have seen some example scripts of variables. But what if you want to access data from user as a input. You must have to save/hold that input in a variable before use it. For this, Bash Shell  gives us facility to save/hold the data provided by user by using  read  command.

      On Unix-like operating systems, read is a builtin command of the Bash shell. It reads a line of text from standard input and splits it into words. These words can then be used as the input for other commands.

In this post we will learn how to use read command in our shell script in order provide input from users.

Syntax:  read "variable_name"

As you see in above syntax, you have to use a variable name in order save the input provided by user. Now see below a simple example of using read command direct from bash shell CLI

$ read name
$ echo "My Name is ${name}"
$ My Name is akay

If you notice that when you assign name as a variable with read command , it will hold the screen for user  and wait for the input provided by user. and as soon as you provide input and hit enter it will give back your shell prompt.

Now if you use that variable in your command as we use it in echo you will get that input data provided by user in the execution of echo command.


bash shell scripting with user input read command

So we can say , in above example name is a variable which hold the data provide by user as input.

Script Example : A simple script to resolve the Domain Name System (DNS) to obtain domain name or IP address mapping, or other DNS records.

Script Explanation :

1) line 5: if there is an error in script , just exit.

2)  line 6: creating a path variable, which will specify the directory path.

3) line 7: creating a file variable, which will specify the name of text file. 

-The file name starts with dnsrecord 

-After initial name, it contains the date command , which will helpful to generate the a unique name every time you execute the script.

- At last specify the extension of file .txt

-This way every time it will generate a txt file with specific name e.g dnsrecord_20200826162048.txt

4) line 8 : here we are asking user to enter the domain name or IP address which we have to check to resolve.

5) line 9 : here using read  , which will hold the input from user in dns variable.

6) line 11: A simple nslookup command , which will obtain the details of given domain name by user

-  ${dns} part will put the domain name which was provided by user in previous read command  as a input.

-  (  > ) this is called redirection which is used to redirect the output data to any other location (like in any text file).

${path} part will put the path which we have defined in our path variable 

-  /(${file} part will define the file name generated by our file variable under the path location.

7) line 12,13 : Simple tells user where the file has been saved and command is successfully executed without any error.

8) save the file with .sh extension , give execution permission & execute the script with absolute/relative path.


bash shell scripting with user input read command

9) Find the file under /var/tmp directory

bash shell scripting with user input read command

10) open file with cat command.

bash shell scripting with user input read command

So now we have learn that how can we use variables & read command and how helpful it is in bash shell scripting.

Task: Create a script by asking to input user a file name & find that file in specified directory. 

Hint: use locate / find  command to search file

If you have any doubt or want to add some thing in the post or any correction please feel free to tell us in comment section.

Please share this post with your friends 😀

bash shell scripting with user input read command


Post a Comment