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.
Comments ( 0 )
No comments available