I want to monitor the diskspace of the /home/ directory NOT to exceed 80%.
I want a script_file.sh for this.
I am trying "df -h" command and I am stuck unable to extract the percentage.
HOW TO extract the percentage (I believe 5th column)?
IF we were able to extract the column, will it return just the value or the value with "%" sign?
I want this value to be tested in a IF condition to check its >80%
Please help me quick, its urgent!!
Thanks
You better use df -kP
or df -k
in a script.
The % in bash/ksh/ash/psh can be chopped off.
percent=`df -kP /home/. | awk 'NR>1 {print $5}'`
percent=${percent%*%}
or
percent=`df -kP /home/. | (read _; read _ _ _ _ percent _; echo ${percent%*%})`
1 Like
thanks, but what does NR>1 do? and what is ${percenta%*%}
does it return only one value of the entire /home/ directory ? coz I need that.
will
IF [ $( df -kP /home | awk '{print $5}' ) -gt 80]; then
echo "exceeds"
else
echo "don't"
FI
this work?
RudiC
March 14, 2016, 5:45am
4
NR > 1
skips the first (= header) line in awk
. shell's parameter expansion ${percent%*%}
(no "a"!) removes the trailing %
sign.
It will not, due to syntactical and semantical errors:
IF should be lower case
the closing ]
needs a leading space
the command substitution needs double quotes
the test
( or [
) command can't handle multiline arguments, it needs a single item output from the command substitution.
the -gt
comparison can't handle the %
sign
1 Like
${percent%*%}
is a variable modifier. So you must store it in a variable first.
percent=`df -kP /home/. | awk 'NR>1 {print $5}'`
if [ ${percent%*%} -gt 80 ]; then
echo "exceeds"
else
echo "ok"
done
Or
if [ `df -kP /home/. | (read _; read _ _ _ _ percent _; echo ${percent%*%})` -gt 80 ]
then
echo "exceeds"
else
echo "ok"
fi
The NR>1
or the read _
skip the first line (header).
if [ $( df -kP /home | awk 'NR>1 {print $5}' ) -gt 80 ]; then
echo "exceeds"
else
echo "don't"
fi
this might work if we can remove the % sign..
can't we remove without storing into a variable?
IF not then as per ur first suggestion,
if [ percentage -gt 80 ]; then
echo "exceeds";
else
echo "dont "
fi
this will work anyway right?
RudiC
March 14, 2016, 5:56am
7
How about
df -kP /home | awk 'NR>1 {print ($5+0 > 80)?"exceeds":"doesn''t"}'
1 Like
Rudic,
You are taking 6th column? I need 5th as it gives the percentage usage.
RudiC
March 14, 2016, 6:21am
9
No, it's adding 0 to make it numeric (i.e. drop the %
sign).
I'm sorry - I made a typo when writing $5+1
- should read $5+0
. Corrected in post#7.