• 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 use Flags by Bash Script on Rocky Linux 9.2

  • 00:31 Cat /etc/os-release
  • 00:47 vim flags.sh
  • 04:19 chmod +x flags.sh
  • 04:30 ll
  • 04:45 ./flags.sh -a
  • 05:02 ./flags.sh -b
  • 05:21 ./flags.sh -c
{{postValue.id}}

To Use Flags By Bash Script On Rocky 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="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

Step 2: Create a Script file using your favourite editor, here iam using vim editor

[root@Linuxhelp ~]# vim flags.sh
Insert the following script in this file
#!/bin/bash
mem(){
    free -h
}
disk(){
     df -Th
}
sys(){
    cat /etc/os-release
}
while getopts 'abc' FLAGS; do
    case "$FLAGS" in
        a)
mem
           ;;
        b)
           disk
           ;;
        c)
           sys
           ;;
    esac
done

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

[root@Linuxhelp ~]# chmod +x flags.sh

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

[root@Linuxhelp ~]# ll
total 12
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root 4096 Oct 16 20:19 Documents
drwxr-xr-x. 2 root root   24 Oct 13 14:20 Downloads
-rwxr-xr-x. 1 root root  215 Oct 16 20:27 flags.sh
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Public
drwxr-xr-x. 2 root root    6 Oct  7 18:30 sample
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root    6 Oct  7 20:15 testdir
drwxr-xr-x. 2 root root    6 Aug 13 22:33 Videos

Step 5: Now run the script with -a flag (Which shows the memory usage in mebibytes.) by using the below command

[root@Linuxhelp ~]# ./flags.sh -a
               total        used        free      shared  buff/cache   available
Mem:           3.5Gi       1.8Gi       580Mi        22Mi       1.4Gi       1.7Gi
Swap:          2.0Gi          0B       2.0Gi

Step 6: Now run the script with -b flag (Which display information about total space and available space on a file system) by using the below command

[root@Linuxhelp ~]# ./flags.sh -b
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs               tmpfs     1.8G     0  1.8G   0% /dev/shm
tmpfs               tmpfs     728M  9.7M  718M   2% /run
/dev/mapper/rl-root xfs        17G  5.4G   12G  32% /
/dev/nvme0n1p1      xfs      1014M  392M  623M  39% /boot
tmpfs               tmpfs     364M  100K  364M   1% /run/user/0

Step 7: Now run the script with -c flag ( which get the information of the version of our operating system) by using the below command

[root@Linuxhelp ~]# ./flags.sh -c
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"

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 Rocky Linux 9.2. Your feedback is much welcome.

Tags:
sebastian
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How do we define and use flags in a Bash Script?

A

You can define and use flags in a Bash script by using the getopts function or by manually processing the arguments with a loop. The getopts function is a built-in option for parsing command-line arguments.

Q

How do we 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

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

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

How can we 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.

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 Isaac ?
How to run windows application in linux

I need to run the windows application in my Linux machine, instead of installing from yum repo or any other repos. How to do that..??

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.