How to create a Bash Script to get User Data on Rocky Linux 9.2

To Create A Bash Script To Get User Data On Rocky Linux 9.2

Introduction:

To read 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 ~]# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Step 2: Create a script file named userdata.sh by using vim text editor

[root@Linuxhelp ~]# vim userdata.sh
Add the following script
#!/bin/bash
echo Hello, What is the your Name?
read Name
echo What is your Age?
read Age
echo what is your Qualification?
read Degree
echo Where are you From?
read Place
echo Name : $Name >> file.txt
echo Age : $Age >> file.txt
echo Qualification : $Degree >> file.txt
echo Place : $Place >> file.txt

Step 3: Long list the files by using the below command

[root@Linuxhelp ~]# ll
total 12
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 4 root root   51 Oct 25 15:24 Desktop
drwxr-xr-x. 2 root root 4096 Oct 25 15:25 Documents
drwxr-xr-x. 2 root root   24 Oct 13 14:20 Downloads
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
-rw-r--r--. 1 root root  315 Oct 25 15:26 userdata.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 4: Give the executable permission for the script file by using the below command

[root@Linuxhelp ~]# chmod +x userdata.sh

Step 5: Execute the script file by using ./command and provide the informations by using the below command.

[root@Linuxhelp ~]# sh userdata.sh
Hello, What is the your Name?
Nishanth
What is your Age?
22
what is your Qualification?
B.Tech
Where are you From?
India

Step 6: Again long list the file and check whether the file.text file is present or not.

[root@Linuxhelp ~]# ll
total 16
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 4 root root   51 Oct 25 15:24 Desktop
drwxr-xr-x. 2 root root 4096 Oct 25 15:25 Documents
drwxr-xr-x. 2 root root   24 Oct 13 14:20 Downloads
-rw-r--r--. 1 root root   64 Oct 25 15:27 file.txt
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
-rwxr-xr-x. 1 root root  315 Oct 25 15:26 userdata.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 7: View the saved files by using the below command

[root@Linuxhelp ~]# vim file.txt
Name : Nishanth
Age : 22
Qualification : B.Tech
Place : India
Our given datas are saved in the file.txt file

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 on Rocky Linux 9.2. Your feedback is much welcome.

FAQ
Q
I have 2 files and I want to print the records that are common to both.
A
We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.
Q
What is $() in Bash?
A
$( command ) or. ` command ` Bash performs the expansion by executing the command in a subshell environment and replacing the command substitution with the standard output of the command, with any trailing newlines deleted.
Q
How do I wait for user input in the Bash script?
A
Using the 'read' Command
The 'read' command is a powerful built-in Bash tool that allows you to read a line of input from the user.
Q
How do bash scripts work?
A
A bash script is a file containing a sequence of commands that are executed by the bash program line by line. It allows you to perform a series of actions, such as navigating to a specific directory, creating a folder, and launching a process using the command line.
Q
How can you collect user input in a Bash script?
A
We have used the read command to get the user input in the variable name. The echo command is an optional command to just verify that we have stored the input in the name variable. We use $ in front of the variable command to fetch and parse the literal value of the variable.