3

I am using the following script to get memory usage value, I want this value in variable so that i can apply 'if' condition on that value.Please suggest

@echo off
free -m | awk 'NR==2{printf "Memory Usage: %s/%sMB (%.2f%)\n", $3,$2,$3*100/$2 }'

1 Answer 1

4

You might want

memory_usage=`free -m | awk 'NR==2{print $3*100/$2 }'`
if [ `echo "${memory_usage} > 90.0" | bc` -eq 1 ] ; then
    echo " > 90.0 "
else
    echo " <= 90.0 "
fi

Note how to save the result of command into variable, and how to compare floating-point number.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick reply, can you please guide me how i can store only the numeric value in a variable and apply if condition on it. Thanks
@MohsinInayatKhan Done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.