How to create a Bash Script to get User Data on Ubuntu 22.04

To Create A Bash Script To Get User Data On Ubuntu 22.04

Introduction

To store the Bash user input, we use the built-in Bash command called read. It takes input to read the Bash user input, we use the built-in Bash command called read. It takes input from the user and assigns it to the variable. It reads only a single line from the Bash shell from the user and assigns it to the variable.

Procedure

Step 1: Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Create a file by using your favourite editor, here I’m using vim editor

root@linuxhelp:~# vim userdata.sh
Copy the following lines in the file
#!/bin/bash
echo "Enter a value"
read
echo "you entered valus is $REPLY

Step 3: Give execute permission to the file by using the below command

root@linuxhelp:~# chmod +x userdata.sh

Step 4: Run the script using ./ by using the below command

root@linuxhelp:~# ./userdata.sh
Output
Enter a value
10
you entered valus is 10

Step 5: Create another file by using your favourite editor, here I’m using vim editor

root@linuxhelp:~# vim userdata1.sh
Copy the following lines in the file
#!/bin/bash
echo "Please enter three numbers"
read X Y Z
ADD=$((X+Y+Z))
echo "Addition of the above values is $ADD"
MULTIPLE=$((X*Y*Z))
echo "Multiplication of the above values is $MULTIPLE"

Step 6: Give execute permission to the file by using the below command

root@linuxhelp:~# chmod +x userdata1.sh

Step 7: Run the script using ./ by using the below command

root@linuxhelp:~# ./userdata1.sh
Please enter three numbers
2 2 2
Addition of the above values is 6
Multiplication of the above values is 8

Conclusion

We have reached the end of this article. In this guide, we have walked you through the steps required to create a Bash Script to get User Data. Your feedback is much welcome.

FAQ
Q
What is a variable in a shell script?
A
A shell variable is a character string in a shell that stores some value.
Q
What is the use of the echo command?
A
The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments.
Q
What is shebang (#!)in Linux?
A
shebang (#!) is a special line at the beginning of a script that tells the operating system which interpreter to use when executing the script.
Q
How do you write data to a file?
A
Use redirection operators to fetch the data from the stdout and stderr streams and redirect them to a text file.
Q
How to read user input into a variable in bash?
A
To read the Bash user input, we use the built-in Bash command called read.