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.