Bash string to int

bashstring

Here is what I tried doing:

#!/bin/sh
res=`df | awk 'FNR == 2 { print $5 }'`
res2=$((res+0))

Here is my error:

-bash: 53%: syntax error: operand expected (error token is "%")

I Googled 'string to int bash' which showed me $((res+0)) but it doesn't work due to the % in the string (53%). How do I convert it in such a way I can do an if statement such as the one below (but that works)?

if [ "$res2" -ge 50 ]
then
    echo "Your disk >=50% full!"
fi

Best Answer

Let awk do it:

df -P | awk 'NR == 2 { print $5+0; exit }'
Related Question