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

To create a Bash Script to get User Data

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.

Installation steps:

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

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2

Step 2: Create a script file use of read command without variable by using the Vim command

[root@linuxhelp ~]# vim userdata1
#!/bin/bash
echo "What is your favorite programming language?"
# Take input without defining variable
read
# Print the input value
echo "Your answer is $REPLY"

Step 3: Execute the script first change the executable file permission by using the below command

[root@linuxhelp ~]# chmod +x userdata1

Step 4: List the file by using the below command

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  174 Sep  7 11:46 userdata1

Step 5: Execute the script by using the below command

[root@linuxhelp ~]# ./userdata1
What is your favorite programming language?
Linux
Your answer is Linux

Step 6: Create a script file using read command with options by Vim command

[root@linuxhelp ~]# vim userdata2
#!/bin/bash
# Type your Login Information
read -p 'Username: ' user
read -sp 'Password: ' pass

# Check the username and password are valid or not
if (( $user == "admin" && $pass == "12345" ))
then
    echo -e "\nSuccessful login"
else
    echo -e "\nUnsuccessful login"
fi

Step 7: Execute the script first change the executable file permission by using the below command

[root@linuxhelp ~]# chmod +x userdata2

Step 8: List the file by using the below commands

[root@linuxhelp ~]# ll
total 12
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  174 Sep  7 11:46 userdata1
-rwxr-xr-x. 1 root root  274 Sep  7 12:11 userdata2

Step 9: Execute the script by using the below commands

[root@linuxhelp ~]# ./userdata2
Username: linuxhelp
Password:
Successful login
[root@linuxhelp ~]# ./userdata2
Username: linuxhelp
Password:
Unsuccessful login

Step 10: Create a script file using read command to take multiple inputs by using the Vim command

[root@linuxhelp ~]# vim userdata3
#!/bin/bash
## Taking multiple inputs
echo "Type four names"
read lan1 lan2 lan3 lan4
echo "$lan1 is your first choice"
echo "$lan2 is your second choice"
echo "$lan3 is your third choice"
echo "$lan4 is your fourth choice"

Step 11: To execute the script, first change the executable file permission by using the below command

[root@linuxhelp ~]# chmod +x userdata3

Step 12: List the file by using the below commands

[root@linuxhelp ~]# ll
total 16
-rw-------. 1 root root 1062 Sep  4 17:37 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  174 Sep  7 11:46 userdata1
-rwxr-xr-x. 1 root root  274 Sep  7 12:11 userdata2
-rwxr-xr-x. 1 root root  224 Sep  7 16:34 userdata3

Step 13: Execute the script by using the below commands

[root@linuxhelp ~]# ./userdata3
Type four names
Welcome to Linuxhelp .com
Welcome is your first choice
to is your second choice
Linuxhelp is your third choice
.com is your fourth choice

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

FAQ
Q
Mention the disadvantages of bash scripts
A
Some disadvantages of the bash script are mentioned below:
• It works slower than other languages.
• The improper script can damage the entire process and generate a complicated error.
Q
What are the advantages of using bash scripts?
A
Bash script has many advantages which are described below:
• It is easy to use and learn.
• Many manual tasks that need to run frequently can be done automatically by writing a bash script.
• The sequence of multiple shell commands can be executed by a single command.
• Bash script written in one Linux operating system can easily execute in other Linux operating systems. So, it is portable
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 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.