How to pass multiple arguments to multiple Flags using Bash Script on Oracle Linux 9.2

To Pass Multiple Arguments To Multiple Flags Using Bash Script On Oracle Linux 9.2

Introduction:

In Bash scripting, flags are used to specify options for a script. Arguments are the values that are provided to those flags. When passing multiple arguments to a flag, you can either use a space-separated list of arguments or an array.

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 named multiple by using vim text editor.

[root@linuxhelp ~]# vim multiple
Insert the following Line 
# !/bin/bash
case $1 in
     --number|-n)
         shift
         echo "You entered number as: $1"
         shift
         ;;
     --collect|-c)
         shift
         echo "You entered collect as: $1"
         ;;
     --timeout|-t)
        shift
        echo "You entered timeout as: $1"
         ;;
     *)
        show_usage
        ;;
esac
~

Step 3: Now give the executable permission to the script file by using the below command.

[root@linuxhelp ~]# chmod +x multiple

Step 4: Again long list the files to check the permission of the script file which has given below

[root@linuxhelp ~]# ll
total 8
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  347 Nov 30 02:36 multiple
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. 2 root root    6 Nov 13 07:49 Videos

Step 5: Again run the script with the flags which is mentioned in the script file by using the below command.

[root@linuxhelp ~]# ./multiple -n 5
You entered number as: 5

Step 6: Again run the script with the flags which is mentioned in the script file by using the below command.

[root@linuxhelp ~]# ./multiple -t 63
You entered timeout as: 63

Step 7: Again run the script with the flags which is mentioned in the script file by using the below command.

[root@linuxhelp ~]# ./multiple -c 6
You entered collect as: 6

Conclusion:

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

FAQ
Q
Can I pass flags with or without values?
A
Yes, flags can be designed to accept values or be used as boolean switches without values. Adjust the script to handle both cases.
Q
Should I provide a help option?
A
Yes, include a help option (-h or --help) that provides information on script usage and explains available flags to make the script user-friendly.
Q
How do I handle unknown flags?
A
Include a default case in the case statement to handle unknown flags, displaying an error message and usage instructions.
Q
Can I pass flags in any order?
A
Yes, when manually parsing arguments, you can pass flags in any order. However, with tools like getopts, the order of flags may matter.
Q
How do I pass multiple arguments to a Bash script using flags?
A
Use a while loop and case statement to manually parse the command-line arguments. Assign values based on flags and use shift to move through the argument list.