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.

FAQ
Q
Can I provide a usage or help message to users when they enter unsupported flags?
A
Yes, it's a good practice to include a usage or help message that guides users on how to correctly use your script. You can display this message when unsupported flags are encountered.
Q
Should Bash script support both short and long flags in my script?
A
Supporting both short and long flags can make your script more user-friendly. You can use a case statement or similar methods to handle both types of flags.
Q
What is the purpose of the getopts command in Bash scripting?
A
getopts is a built-in command in Bash used for parsing command-line options. It simplifies the process of handling flags and their arguments in a script.
Q
How can I define supported flags in my Bash script?
A
You can define supported flags by creating an array or list that contains the flags your script recognizes. This list helps you verify incoming flags against supported options.
Q
What are unsupported flags in a Bash script?
A
Unsupported flags are command-line options or arguments that are not recognized or supported by the script. When users provide unsupported flags, the script should handle them gracefully.