Link to home
Start Free TrialLog in
Avatar of Maheshbabu Gadde
Maheshbabu Gadde

asked on

Getting error for the above script

#!/bin/bash

current_usage=$( df -h | grep 'Filesystem' | awk {'print $5'} )
max_usage=80%

if [ ${current_usage%?} -ge ${max_usage%?} ]; then
    mailbody="Max usage exceeded. Your disk usage is at ${current_usage}."
    echo "Sending mail..."
    echo ${mailbody} | mail -s "Disk alert!" "xxx@yy.com"
elif [ ${current_usage%?} -lt ${max_usage%?} ]; then
    echo "No problems. Disk usage at ${current_usage}." > /dev/null
fi

Getting integer expression expected error at line-4:
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

Current update to remove only following error:
             Getting integer expression expected error at line-4:
 #!/bin/bash
current_usage=$( df -h | grep 'Filesystem' | awk {'print $5'} )
max_usage=80%
if [[ ${current_usage%?} -ge ${max_usage%?} ]]; then
    mailbody="Max usage exceeded. Your disk usage is at ${current_usage}."
    echo "Sending mail..."
    echo ${mailbody}
elif [[ ${current_usage%?} -lt ${max_usage%?} ]]; then
    echo "No problems. Disk usage at ${current_usage}." > /dev/null
fi

Open in new window

Writing script now to remove all exceptions.
Related script:
#!/bin/bash
ERRFOUND=""
for RequiredFile in /usr/bin/whoami \
/bin/df \
/bin/grep \
/bin/sort \
/usr/bin/awk \
/usr/bin/head \
/usr/bin/mail
do
	if [[ ! -f $RequiredFile ]]
	then
		echo "Update this script using correct location for $RequiredFile"
		ERRFOUND=1
	fi
done
if [[ -z "$ERRFOUND" ]]
then
	WHOAMI=/usr/bin/whoami
	DF=/bin/df
	GREP=/bin/grep
	SORT=/bin/sort
	AWK=/usr/bin/awk
	HEAD=/usr/bin/head
	MAIL=/usr/bin/mail
	LOGNAME=$($WHOAMI)
	HOSTNAME=localhost
	CUR_MAX_USAGE_MOUNTED_ON=$($DF -h | $SORT -k 5 -nr | $GREP -v 'Filesystem' | $HEAD -1)
	CUR_USAGE=$(echo $CUR_MAX_USAGE_MOUNTED_ON | $AWK '{ print $5}')
	MAX_USAGE_HEAD=$($DF -h | $HEAD -1 |\
		$AWK '{
			printf( "%10s %7s %7s %7s %7s %s %s", $1, $2, $3, $4, $5, $6, $7);
	}')
	CUR_MAX_USAGE_MOUNTED_ON=$(echo -n $CUR_MAX_USAGE_MOUNTED_ON |\
		$AWK '{
			printf( "%10s %7s %7s %7s %7s %s\n", $1, $2, $3, $4, $5, $6);
	}')
	MAX_USAGE=80%
	if [[ ${CUR_USAGE%?} -ge ${MAX_USAGE%?} ]]
	then
		EMAIL_CONTENT="Hi $LOGNAME,\n\nMax usage exceeded:\n$MAX_USAGE_HEAD\n$CUR_MAX_USAGE_MOUNTED_ON\n\nRegards,\nAutomated email"
		echo "Sending mail..."
		echo -ne "$EMAIL_CONTENT" | $MAIL -s "Disk alert!" "$LOGNAME@localhost"
		MAILRET=$?
		if [[ 0 -ne $MAILRET ]]
		then
			echo "Using $MAIL failed";
			echo "Return value being $MAILRET"
		fi
	elif [[ ${CUR_USAGE} -lt ${MAX_USAGE%?} ]]
	then
		echo "No problems. Disk usage at ${CUR_USAGE%}." > /dev/null
	fi
fii

Open in new window

1. Always use full path
2. Check existence of executable before executing that.
3. Handle all exceptions using $?
4. Modified email using $LOGNAME@localhost instead of xxx@yy.com => for testing the same.
Change that based on requirement
I think the problem is in the line
max_usage=80%

It should simply be
max_usage=80

It is expecting an integer, but the percent symbol is causing it to fail.
@Maheshbabu Gadde

Thank you for clicking endorse.
Click Accepted/assisted solution for current query.
Reason for writing the comment:
Forced accept/assist if inactive more than 14 days.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.