How to use Flags by Bash Script on Oracle Linux 9.2

To Use Flags By Bash Script On Oracle Linux 9.2

Introduction:

Handling flags in Bash scripts allows users to customize the script’s behaviour and provides greater flexibility and usability. The getopts command is suitable for handling short options, while an extended approach enables handling both short and long options.

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 by using the below command

[root@Linuxhelp ~]# vim flags
Insert the following script in this file
#!/bin/bash
while getopts 'lha:' OPTION; do
  case "$OPTION" in
    l)
      echo "linuxconfig"
      ;;
    h)
      echo "you have supplied the -h option"
      ;;
    a)
      avalue="$OPTARG"
      echo "The value provided is $OPTARG"
      ;;
    ?)
      echo "script usage: $(basename \$0) [-l] [-h] [-a somevalue]" >&2
      exit 1
      ;;
  esac
done
shift "$(($OPTIND -1))"

Step 3: Give the executable permission to the script file by using the below command

[root@Linuxhelp ~]# chmod +x flags

Step 4: Check the execute permission by listing the directory by using the below command

[root@linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1067 Nov 13 07:39 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Desktop
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Documents
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Downloads
-rwxr-xr-x. 1 root root  386 Nov 28 02:34 flags
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Music
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Pictures
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Public
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Templates
drwxr-xr-x. 4 root root   44 Nov 23 17:37 test
drwx--xr--. 2 root root    6 Nov 23 17:15 test1
drwxr-xr-x. 2 root root    6 Nov 13 07:49 Videos

Step 5: Now run the script with -l option, and the script printed onscreen the string we set in the corresponding case by using the below command

[root@Linuxhelp ~]# ./flags -l
linuxconfig

Step 6: Now run the script printed onscreen the string we set in the corresponding case, this is also what happens if we provide the -h option by using the below command

[root@Linuxhelp ~]# ./flags -h
you have supplied the -h option

Step 7: Let’s now try to call the script with the -a option. As said above, this option requires an argument, and will fail if the latter is not provided by using the below command

[root@Linuxhelp ~]# ./flags -a
./flags: option requires an argument -- a
script usage: $0 [-l] [-h] [-a somevalue]

Step 8: Let’s now try to call the script with the -a option. As said above, this option requires an argument, and will execute if the letter is provided by using the below command

[root@Linuxhelp ~]# ./flags -a Linuxhelp
The value provided is Linuxhelp

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Flags by Bash Script on Oracle Linux 9.2. Your feedback is much welcome.

FAQ
Q
How can I make my Bash script more robust when handling flags?
A
To make your script more robust, consider error handling and validation for flags. Check if required flags are provided, validate flag values, and provide informative error messages.
Q
Are there any libraries or tools for working with flags in Bash scripts?
A
Yes, there are libraries like "getopts," "docopt," and "argparse" for handling command-line arguments and flags more conveniently. These libraries provide features for parsing and validating input in a user-friendly way.
Q
What's the difference between short and long flags in Bash scripts?
A
Short flags are typically a single character preceded by a single hyphen, e.g., -a. Long flags are longer and more descriptive, often preceded by two hyphens, e.g., --verbose. They serve a similar purpose but are more user-friendly and self-explanatory.
Q
How do I pass flags when running a Bash script?
A
When running a Bash script, you pass flags by specifying them on the command line when you execute the script. Flags are typically preceded by a hyphen, e.g., -a or -b value.
Q
How do I define and use flags in a Bash script?
A
You can define and use flags in a Bash script using the getopts function or manually processing the arguments with a loop. The getopts function is a built-in option for parsing command-line arguments.