Bash Wizards - How to execute a Option within a Menu Display Script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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.
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.
#!/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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.