Bash Wizards - How to execute a Option within a Menu Display Script
by LinuxRSA from LinuxQuestions.org on (#6CADW)
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 -tThis 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";;
esacThis 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) ExitQuestion 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
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 -tThis 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";;
esacThis 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) ExitQuestion 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