• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

  • 00:38 cat /etc/os-release
  • 00:53 vim multiple
  • 02:49 chmod +x multiple
  • 02:58 ll
  • 03:10 ./multiple -n 5
  • 03:24 ./multiple -t 63
  • 03:40 ./multiple -c 6
{{postValue.id}}

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.

Tags:
matthew
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

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.

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 handle unknown flags?

A

Include a default case in the case statement to handle unknown flags, displaying an error message and usage instructions.

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

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.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Elijah ?
Remote Desktop Connection Has Stopped Working

When accessing my remote machine server using remote desktop on a windows machine I am getting this error

forum (1)

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.