Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi i got the following code #!/bin/sh # Set to email address to send to EMAILADD

ID: 3849002 • Letter: H

Question

Hi i got the following code

#!/bin/sh
# Set to email address to send to
EMAILADDR="test@test.com"

# Set warning percentage
ALERT=10

# Set critical warning percentage
CRITALERT=30

# Get the listing of filesystems and put in $output
df -h |egrep -v '^Filesystem|tmpfs|cdrom|proc|ufs|ext2' | awk '{ print $5 " " $6}'|sed 's/%//g' | while read output;
do
echo $output
# Grab just the percentage
usep='echo $output | awk ' printf $1 ''
# Grab the partition
partition='echo $output | awk ' printf $2' '
# First check for critical since it's higher value
if [[ "$usep" -ge "$CRITALERT" ]]; then
# echo the body to the mail program (-s is subject) and send
echo "Running out of space "$partition ($usep%)" on $(hostname) as on $(date)" |
mail -s "Critical Warning: Warning: $partition is at $usep% of capacity" $EMAILADDR
# If it's not above critical, test to see if it's above alert
elif [[ "$usep" -ge "$ALERT" ]]; then
echo "Running out of space "$partition ($usep%)" on $(hostname) as on $(date)" |
mail -s "Warning: $partition is at $usep% of capacity" $EMAILADDR
fi
done

right now is sending multiple emails instead of those that only apply the 10 and 30 citeria and the email im gettign is blank where it should put the porcentage and the partition name

i think is because is not recognizing usep variable as a integer for the comparison done in the if and else statement

could anyone assist

Explanation / Answer

My take on this is if a variable contents are to shown via echo then the variable can be mentioned as $Variable inside echo command.
But if contents of the variable has to be used in non-echo context then variable should be enclosed within curly braces like
${variable}.

This is for the example how it looks like:
Today_Mail_Send=`ls -ltr /archivelogs/User1/AMPS/${AMSPD_File}_MailStatus | wc -l`

Here within the context of ls command contents of variable AMPD_File is used.

Your situation is also similar since you are mentioning $partition and $usep outside the context of echo command. Once outside curly braces has to be used.

I believe the below solution should work.


echo "Running out of space "${partition} (${usep}%)" on $(hostname) as on $(date)" |
      mail -s "Warning: ${partition} is at ${usep}% of capacity" $EMAILADDR

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote