3

I am writing the bash script which will show the disk usage status. If the size of the filesystem is used more than 95% then that line should be highlighted only one time like below. Basically, I want my script to work same as command df -h but with highlighting maximum disk usage. And other filesystems also should be on output. Here is my script.

readarray -t disk <<< "$(df -h | awk '{print $5}' | tail -n +2 | tr -d %)" for i in "${disk[@]}" do if [ $i -gt 95 ] then df -h | grep --color -E "$i%|$" fi done 
 # df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 140M 3.8G 4% /dev/shm tmpfs 3.9G 1.8M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/centos-root 128G 107G 15G 96% / tmpfs 3.9G 15M 3.9G 1% /tmp /dev/sda1 453M 179M 247M 42% /boot tmpfs 789M 20K 789M 1% /run/user/42 tmpfs 789M 60K 789M 1% /run/user/1000 /dev/mapper/centos-home 100G 85G 16G 96% /mnt 

1 Answer 1

2

This is working for me. You can also color the output if you want.

#! /bin/bash - TXT_BLD=$(tput bold) TXT_RST=$(tput sgr0) IFS= mapfile -t disk < <(df -h | tail -n +2) for line in "${disk[@]}"; do USAGE=$(echo $line | awk '{print $5/1}') if [[ "$USAGE" -gt '95' ]]; then echo "${TXT_BLD}$line${TXT_RST}" else echo "$line" fi done 

This will make the line yellow if greater than 85% and red if greater than 95%:

#! /bin/bash - TXT_BLD=$(tput bold) TXT_RED=$(tput setaf 1) TXT_YLW=$(tput setaf 3) TXT_WARN="${TXT_BLD}${TXT_YLW}" TXT_ERR="${TXT_BLD}${TXT_RED}" TXT_RST=$(tput sgr0) IFS= mapfile -t disk < <(df -h | tail -n +2) for line in "${disk[@]}"; do USAGE=$(echo $line | awk '{print $5/1}') if [[ "$USAGE" -gt '95' ]]; then echo "${TXT_ERR}$line${TXT_RST}" elif [[ "$USAGE" -gt '85' ]]; then echo "${TXT_WARN}$line${TXT_RST}" else echo "$line" fi done 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.