How to use Flags by Bash Script on Rocky Linux 9.2
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.
Comments ( 0 )
No comments available