LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-20-2023, 04:32 AM   #1
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Rep: Reputation: Disabled
Bash Wizards - How to execute a Option within a Menu Display Script


Hi Bash Wizards.

Im creating a bash clean up script that performs housekeeping on disks ( Ubuntu )

I have a list of OS / APP one liners that performs cleanups.

I have written the below bash scripts to be executed as root user.

1. Display the File System.

Code:
#!/bin/bash
##Disk Clean up script

MOUNT=$(mount|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -u -t' ' -k1,2)
FS_USAGE=$(df -PTh|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')
IUSAGE=$(df -PThi|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')

#--------Check for any read-only file systems--------#
echo -e "\nChecking For Read-only File System[s]"
echo -e "$D"
echo "$MOUNT"|grep -w \(ro\) && echo -e "\n.....Read Only file system[s] found"|| echo -e ".....No read-only file system[s] found. "

#--------Check for currently mounted file systems--------#
echo -e "\n\nChecking For Currently Mounted File System[s]"
echo -e "$D$D"
echo "$MOUNT"|column -t

#--------Check disk usage on all mounted file systems--------#
echo -e "\n\nChecking For Disk Usage On Mounted File System[s]"
echo -e "$D$D"
echo -e "( 0-79% = OK/HEALTHY, 80-89% = WARNING, 90-100% = CRITICAL )"
echo -e "$D$D"
echo -e "Mounted File System[s] Utilization (Percentage Used):\n"

echo "$FS_USAGE"|awk '{print $1 " "$7}' > /tmp/s1.out
echo "$FS_USAGE"|awk '{print $6}'|sed -e 's/%//g' > /tmp/s2.out
> /tmp/s3.out

for i in $(cat /tmp/s2.out);
do
{
  if [ $i -ge 90 ];
   then
     echo -e $i"% ------------------Critical" >> /tmp/s3.out;
   elif [[ $i -ge 80 && $i -lt 89 ]];
   then
     echo -e $i"% ------------------Warning" >> /tmp/s3.out;
   else
     echo -e $i"% ------------------Healthy" >> /tmp/s3.out;
  fi
}
done
paste -d"\t" /tmp/s1.out /tmp/s3.out|column -t
This is the Output:

Code:
root@disk-app-01:/home/housekeeping# ./diskdisplay.sh

Checking For Read-only File System[s]

.....No read-only file system[s] found.


Checking For Currently Mounted File System[s]

/dev/sda1  on  /     type  ext4  (rw,relatime,discard,data=ordered)
/dev/sdb1  on  /mnt  type  ext4  (rw,relatime,data=ordered)


Checking For Disk Usage On Mounted File System[s]

( 0-79% = OK/HEALTHY, 80-89% = WARNING, 90-100% = CRITICAL )

Mounted File System[s] Utilization (Percentage Used):

/dev/sdb1  /mnt  1%   ------------------Healthy
/dev/sda1  /     77%  ------------------Healthy
root@disk-app-01:/home/housekeeping#

I have created the below Menu script:


Code:
#!/bin/bash
echo
   echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
   echo -e '\033[1mWelcome to the Disk Clean up Shop\033[0m'
   echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 
echo 
echo "#Select The Disk Operation Option"
echo
echo "  1) Display Disk Information"
echo
echo "  2) Clear OS Related Files"
echo
echo "  3) Clear Space on APP Nodes"
echo
echo "  4) Clear Space on Ingress Nodes" 
echo
echo "  5) Exit" 

read n
case $n in
  1) echo "You chose Option 1";;
  2) echo "You chose Option 2";;
  3) echo "You chose Option 3";;
  4) echo "You chose Option 4";;
  5) echo "You chose Option 5";;
  *) echo "invalid option";;
esac
This is the output:

Code:
root@disk-app-01:/home/housekeeping#  ./menudisplay.sh

******************************************
Welcome to the Disk Clean up Shop
******************************************

#Select The Disk Operation Option

  1) Display Disk Information

  2) Clear OS Related Files

  3) Clear Space on APP Nodes

  4) Clear Space on Ingress Nodes

  5) Exit
Question 1: How do i do i run the ./menudisplay.sh and select option 1) Display Disk Information by pressing 1.

Question 2: Can one add other options for clean up and return to main menu ?


Thanks
 
Old 06-20-2023, 05:07 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,715

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
You should be able to find lots of examples of how to create a bash menu and how to use the case statement.
The basic syntax of a case statement is:
Code:
case expression in
 pattern 1) # each pattern and the following statements are called a clause
    statements
;; #separates clauses
 pattern 2)
   statements
;;
...
esac
You can add your specific commands to run for each clause or put them in a function to be called.

Put the menu inside a continuous loop that runs until you exit. You can add matching patterns as desired.

bash also has a built in select command for menus.
 
Old 06-20-2023, 09:27 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by LinuxRSA View Post
Question 1: How do i do i run the ./menudisplay.sh and select option 1) Display Disk Information by pressing 1.
Sorry, I don't quite understand your first question. Doesn't your script already do this?

Last edited by shruggy; 06-20-2023 at 11:06 AM.
 
Old 06-20-2023, 09:54 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
I think he's asking "how do I select option one by only pressing '1'". If so, the answer is use read -rN1

I'm not sure what he's asking in Q2, as the answer is obviously "Yes, one can."
 
Old 06-20-2023, 09:55 AM   #5
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Sorry, I don't quite understand your first question. Doesn't your script already does this?
I have two scripts:

1 - A bash script to display disk info - ./diskdisplay.sh
2 - A bash script that creates a menu - ./menudisplay.sh


I want to be able to run the menu script then press 1 which Displays Disk Information the output of ./diskdisplay.sh and display it.

So... I want one script.

This is the code, I just don't know how to make it work

Code:
#!/bin/bash
##Disk Clean up script

echo
   echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
   echo -e '\033[1mWelcome to the Disk Clean up Shop\033[0m'
   echo \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 
echo 
echo "#Select The Disk Operation Option"
echo
echo "  1) Display Disk Information"
echo
echo "  2) Clear OS Related Files"
echo
echo "  3) Clear Space on APP Node"
echo
echo "  4) Clear Space on Ingress Node" 
echo
echo "  5) Exit" 

read n
case $n in
  1) echo "Displaying Disk Information";;
  2) echo "Clearing OS Related Files";;
  3) echo "Clearing Space on APP Nodes";;
  4) echo "Clear Space on Ingress Nodes";;
  5) echo "Exiting Disk Clean up Script - Goodbye";;
  *) echo "invalid option";;
esac   


##Displaying Disk Information

MOUNT=$(mount|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -u -t' ' -k1,2)
FS_USAGE=$(df -PTh|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')
IUSAGE=$(df -PThi|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')

#--------Check for any read-only file systems--------#
echo -e "\nChecking For Read-only File System[s]"
echo -e "$D"
echo "$MOUNT"|grep -w \(ro\) && echo -e "\n.....Read Only file system[s] found"|| echo -e ".....No read-only file system[s] found. "

#--------Check for currently mounted file systems--------#
echo -e "\n\nChecking For Currently Mounted File System[s]"
echo -e "$D$D"
echo "$MOUNT"|column -t

#--------Check disk usage on all mounted file systems--------#
echo -e "\n\nChecking For Disk Usage On Mounted File System[s]"
echo -e "$D$D"
echo -e "( 0-79% = OK/HEALTHY, 80-89% = WARNING, 90-100% = CRITICAL )"
echo -e "$D$D"
echo -e "Mounted File System[s] Utilization (Percentage Used):\n"

echo "$FS_USAGE"|awk '{print $1 " "$7}' > /tmp/s1.out
echo "$FS_USAGE"|awk '{print $6}'|sed -e 's/%//g' > /tmp/s2.out
> /tmp/s3.out

for i in $(cat /tmp/s2.out);
do
{
  if [ $i -ge 90 ];
   then
     echo -e $i"% ------------------Critical" >> /tmp/s3.out;
   elif [[ $i -ge 80 && $i -lt 89 ]];
   then
     echo -e $i"% ------------------Warning" >> /tmp/s3.out;
   else
     echo -e $i"% ------------------Healthy" >> /tmp/s3.out;
  fi
}
done

Last edited by LinuxRSA; 06-20-2023 at 10:11 AM.
 
Old 06-20-2023, 10:59 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
michaelk answered this in #2. Do you need examples? ABS has some. Or, as Bash Guide for Beginners suggests, have a look inside /etc/init.d. Even on a systemd-based system there are quite a few initscripts, and most of them make use of case statements.

Last edited by shruggy; 06-20-2023 at 11:22 AM.
 
Old 06-20-2023, 11:28 AM   #7
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
General idea.

Code:
#!/usr/bin/bash

PS3="
Select an option. : "

options=('Displaying Disk Information'
        'Clearing OS Related Files'
        'Clearing Space on APP Nodes'
        'Clear Space on Ingress Nodes'
        'Exiting Disk Clean up Script - Goodbye'
        'invalid option'
        'Exit')

while :; do
    clear
    select opt in "${options[@]}"; do
        case "$opt" in
            "${options[0]}") echo "You entered - "$opt"" ;;
            
            "${options[1]}") echo "You entered - "$opt"" ;;
            
            "${options[2]}") echo "You entered - "$opt"" ;;
            
            "${options[3]}") echo "You entered - "$opt"" ;;
            
            "${options[4]}") echo "You entered - "$opt"" ;;
            
            "${options[5]}") echo "You entered - "$opt"" ;;
            
            "${options[6]}") echo "Bye"; exit ;;
        esac
        sleep 2
        break
    done
done
 
Old 06-21-2023, 04:56 AM   #8
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
General idea.

Code:
#!/usr/bin/bash

PS3="
Select an option. : "

options=('Displaying Disk Information'
        'Clearing OS Related Files'
        'Clearing Space on APP Nodes'
        'Clear Space on Ingress Nodes'
        'Exiting Disk Clean up Script - Goodbye'
        'invalid option'
        'Exit')

while :; do
    clear
    select opt in "${options[@]}"; do
        case "$opt" in
            "${options[0]}") echo "You entered - "$opt"" ;;
            
            "${options[1]}") echo "You entered - "$opt"" ;;
            
            "${options[2]}") echo "You entered - "$opt"" ;;
            
            "${options[3]}") echo "You entered - "$opt"" ;;
            
            "${options[4]}") echo "You entered - "$opt"" ;;
            
            "${options[5]}") echo "You entered - "$opt"" ;;
            
            "${options[6]}") echo "Bye"; exit ;;
        esac
        sleep 2
        break
    done
done

This works nicely

This is my output when i run the menudisplay script.

Code:
1) Display Disk Information      4) Clear Space on Ingress Nodes
2) Clear OS Related Files        5) Exit
3) Clear Space on APP Nodes

Select an option. :
Question: where do i insert the code to display the disk information ?

Do i insert it between "${options[0]}") echo "You entered - "$opt"" ;;
 
Old 06-21-2023, 05:08 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by LinuxRSA View Post
Question: where do i insert the code to display the disk information ?

Do i insert it between "${options[0]}") echo "You entered - "$opt"" ;;
Why don't you try it yourself?
 
Old 06-21-2023, 05:43 AM   #10
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Why don't you try it yourself?
Managed to get the display sorted.

Code:
#!/bin/bash

MOUNT=$(mount|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -u -t' ' -k1,2)
FS_USAGE=$(df -PTh|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')
IUSAGE=$(df -PThi|egrep -iw "ext4|ext3|xfs|gfs|gfs2|btrfs"|sort -k6n|awk '!seen[$1]++')

PS3="
Select an option. : "
options=('Display Disk Information'
        'Clear OS Related Files'
        'Clear Space on APP Nodes'
        'Clear Space on Ingress Nodes'
        'Exit')

while :; do
    clear
    select opt in "${options[@]}"; do
        case "$opt" in
            "${options[0]}") echo "You entered - "$opt""  ; #--------Check for any read-only file systems--------#
echo -e "\nChecking For Read-only File System[s]"
echo -e "$D"
echo "$MOUNT"|grep -w \(ro\) && echo -e "\n.....Read Only file system[s] found"|| echo -e ".....No read-only file system[s] found. "

#--------Check for currently mounted file systems--------#
echo -e "\n\nChecking For Currently Mounted File System[s]"
echo -e "$D$D"
echo "$MOUNT"|column -t

#--------Check disk usage on all mounted file systems--------#
echo -e "\n\nChecking For Disk Usage On Mounted File System[s]"
echo -e "$D$D"
echo -e "( 0-79% = OK/HEALTHY, 80-89% = WARNING, 90-100% = CRITICAL )"
echo -e "$D$D"
echo -e "Mounted File System[s] Utilization (Percentage Used):\n"

echo "$FS_USAGE"|awk '{print $1 " "$7}' > /tmp/s1.out
echo "$FS_USAGE"|awk '{print $6}'|sed -e 's/%//g' > /tmp/s2.out
> /tmp/s3.out

for i in $(cat /tmp/s2.out);
do
{
  if [ $i -ge 90 ];
   then
     echo -e $i"% ------------------Critical" >> /tmp/s3.out;
   elif [[ $i -ge 80 && $i -lt 89 ]];
   then
     echo -e $i"% ------------------Warning" >> /tmp/s3.out;
   else
     echo -e $i"% ------------------Healthy" >> /tmp/s3.out;
  fi
}
done
paste -d"\t" /tmp/s1.out /tmp/s3.out|column -t ;;

            "${options[1]}") echo "You entered - "$opt"" ;;

            "${options[2]}") echo "You entered - "$opt"" ;;

            "${options[3]}") echo "You entered - "$opt"" ;;

            "${options[4]}") echo "Exiting Disk Clean up Script - Goodbye"; exit ;;
        esac
        sleep 5
        break
    done
done

Output is as follows:

Code:
1) Display Disk Information      4) Clear Space on Ingress Nodes
2) Clear OS Related Files        5) Exit
3) Clear Space on APP Nodes

Select an option. : 1
You entered - Display Disk Information

Checking For Read-only File System[s]

.....No read-only file system[s] found.


Checking For Currently Mounted File System[s]

/dev/sda1  on  /     type  ext4  (rw,relatime,discard,data=ordered)
/dev/sdb1  on  /mnt  type  ext4  (rw,relatime,data=ordered)


Checking For Disk Usage On Mounted File System[s]

( 0-79% = OK/HEALTHY, 80-89% = WARNING, 90-100% = CRITICAL )

Mounted File System[s] Utilization (Percentage Used):

/dev/sdb1  /mnt  1%   ------------------Healthy
/dev/sda1  /     77%  ------------------Healthy

It displays this output for about 5-6 seconds then returns to the main menu.

How does one bypass the returning to main menu and perhaps insert a go back / return to main menu option.
 
Old 06-21-2023, 05:44 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,715

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Code:
while :; do
    clear
    select opt in "${options[@]}"; do
        case "$opt" in
            "${options[0]}") 
               ## Disk Information code goes here.
             ;;
            
            "${options[1]}") 
             ## Clear OS Related Files code goes here.
            ;;
            
            "${options[2]}") 
              ##   Clear Space on APP Node code here
             ;;
            
            "${options[3]}") 
               ## Clear Space on Ingress Nodes code here
             ;;
            
            "${options[4]}") 
               ## exit code goes here i.e.
               exit
            ;;
            
            *) 
              echo "Invalid option $REPLY"
            ;;
        
       esac
    done
done
As posted before you can place each code block in a function and call that function from the case statement.

create a submenu or maybe a press any key wait prompt etc.

Last edited by michaelk; 06-21-2023 at 05:47 AM.
 
Old 06-21-2023, 06:51 AM   #12
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Code:
while :; do
    clear
    select opt in "${options[@]}"; do
        case "$opt" in
            "${options[0]}") 
               ## Disk Information code goes here.
             ;;
            
            "${options[1]}") 
             ## Clear OS Related Files code goes here.
            ;;
            
            "${options[2]}") 
              ##   Clear Space on APP Node code here
             ;;
            
            "${options[3]}") 
               ## Clear Space on Ingress Nodes code here
             ;;
            
            "${options[4]}") 
               ## exit code goes here i.e.
               exit
            ;;
            
            *) 
              echo "Invalid option $REPLY"
            ;;
        
       esac
    done
done
As posted before you can place each code block in a function and call that function from the case statement.

create a submenu or maybe a press any key wait prompt etc.
Life Saver!!! Thank you @michaelk
Script is now running flawlessly.

Now to make it look nice
 
Old 06-21-2023, 07:14 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,715

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Code:
while :; do
    clear
    select opt in "${options[@]}"; do
...
I was not paying enough attention to teckk's code but there is a clear command that is causing your "problems".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Properly execute menu-driven scripts (uses ncurses) within a bash script budrz89 Linux From Scratch 8 05-11-2020 05:22 AM
Shell script with Menu and sub menu and in sub menu execute another shell script SHWE Linux - Newbie 9 11-03-2018 06:19 PM
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
Making menu's within menu's (function) in bash genderbender Programming 1 03-19-2008 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration