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