How to use Flags on Bash Script on Ubuntu 22.04

To Use Flags On Bash Script On Ubuntu 22.04

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:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy

Step 2: Create a file using your favourite editor, here I’m using vim editor

root@linuxhelp:~# vim test.sh
Copy the following script in the file
#!/bin/bash
mem(){
        free -h | grep Mem
}
disk(){
        df -h /
}
load(){
        uptime
}
while getopts 'xyz' OPTION; do
        case "$OPTION" in
                x)
                        mem
                        ;;
                y)
                        disk
                        ;;
                z)
                        load
                        ;;
        esac
done

Step 3: Now long list the directory by using the below command

root@linuxhelp:~# ls -la
total 56
drwx------  4 root root 4096 Nov 15 01:39 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rw-------  1 root root  426 Nov 14 00:48 .bash_history
-rw-r--r--  1 root root 3106 Oct 15  2021 .bashrc
drwx------  2 root root 4096 Sep 11 17:28 .cache
-rwxr-xr-x  1 root root  197 Nov 15 01:20 flag.sh
-rw-------  1 root root   20 Nov 15 01:21 .lesshst
-rw-r--r--  1 root root  161 Jul  9  2019 .profile
drwx------  5 root root 4096 Sep 11 22:24 snap
-rw-r--r--  1 root root  201 Nov 15 01:39 test.sh
-rw-------  1 root root 8936 Nov 15 01:39 .viminfo
-rw-------  1 root root   55 Sep 11 17:28 .Xauthority

Step 4: There is no execute permission for the file, so give execute permission by using the following command

root@linuxhelp:~# chmod +x test.sh

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

root@linuxhelp:~# ls -la
total 56
drwx------  4 root root 4096 Nov 15 01:39 .
drwxr-xr-x 20 root root 4096 Sep 11 22:13 ..
-rw-------  1 root root  426 Nov 14 00:48 .bash_history
-rw-r--r--  1 root root 3106 Oct 15  2021 .bashrc
drwx------  2 root root 4096 Sep 11 17:28 .cache
-rwxr-xr-x  1 root root  197 Nov 15 01:20 flag.sh
-rw-------  1 root root   20 Nov 15 01:21 .lesshst
-rw-r--r--  1 root root  161 Jul  9  2019 .profile
drwx------  5 root root 4096 Sep 11 22:24 snap
-rwxr-xr-x  1 root root  201 Nov 15 01:39 test.sh
-rw-------  1 root root 8936 Nov 15 01:39 .viminfo
-rw-------  1 root root   55 Sep 11 17:28 .Xauthority

Step 6: Now run the script without flags by using the below command

root@linuxhelp:~# ./test.sh

Step 7: Now run the script with -x flag (Which shows the memory usage) by using the below command

root@linuxhelp:~# ./test.sh -x
Mem:           3.8Gi       1.0Gi       1.3Gi        45Mi       1.4Gi       2.5Gi

Step 8: Now run the script with -y flag (Which shows the storage info of the / directory) by using the below command

root@linuxhelp:~# ./test.sh -y
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        39G   13G   24G  36% /

Step 9: Now run the script with -z flag (Which shows the system uptime, No of user and load average) by using the below command

root@linuxhelp:~# ./test.sh -z
 01:41:02 up 35 min,  1 user,  load average: 0.04, 0.07, 0.16

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to use Flags on Bash Script. Your feedback is much welcome.

FAQ
Q
What is the use of the sleep-in bash script?
A
This will pause your script for N seconds, with N being either a positive integer or a floating point number.
Q
Which flag shows verbose output for a bash script?
A
We can enable the verbose mode using the -v switch, which allows us to view each command before it's executed.
Q
How do you handle flags in bash?
A
The getopts command is suitable for handling short options, while an extended approach enables handling both short and long options.
Q
What does the E flag mean in bash?
A
The -e flag in both the Bourne Shell and C shell causes the shell to exit if any command fails.
Q
How to use the flag on the Bash script?
A
Flags usually begin with a single hyphen (-) followed by a single letter or a double hyphen (–) followed by a full word