How to validate Unsupported Flags on Bash Script on Oracle Linux 9.2
To validate Unsupported Flags on Bash Script on Oracle Linux 9.2
Introduction:
Bash scripts are a powerful tool for automating tasks on Linux systems. One of the features of Bash scripts is the ability to accept flags from the command line. Flags are used to modify the behavior of the script and can be used to specify input files, output files, and other options. However, Bash scripts typically only support a limited set of flags. If a user attempts to pass an unsupported flag to a script, the script may fail or behave unexpectedly.
Procedure:
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 using your favourite editor, here I am using vim editor
[root@Linuxhelp ~]# vim unsupported
Insert the following script in this file
# !/bin/bash
while getopts ":hvfd:" opt; do
case $opt in
h) echo "Display help message" ;;
v) echo "Display verbose output" ;;
f) echo "Process input file" ;;
d) echo "Specify data directory: $OPTARG" ;;
?) echo "Invalid option: -$OPTARG" >&2 ;;
esac
done
Step 3: Give the executable permission to the script file by using the below command
[root@Linuxhelp ~]# chmod +x unsupported
Step 4: Check the execute permission by listing the directory
[root@linuxhelp ~]# ll
Step 5: Now execute the script with option -h by using the below command
[root@linuxhelp ~]# ./unsupported -h
Display help message
Step 6: Now execute the script with option -v by using the below command
[root@linuxhelp ~]# ./unsupported -v
Display verbose output
Step 7: Now execute the script with option -f by using the below command
[root@linuxhelp ~]# ./unsupported -f
Process input file
Step 8: Now execute the script with option -d for invalid option by using the below command
[root@linuxhelp ~]# ./unsupported -d
Invalid option: -d
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to Validate Unsupported Flags on Bash Script on Oracle Linux 9.2. Your feedback is much welcome.
Comments ( 0 )
No comments available