0

I have to write a piece of code which compares the available disk space with a pre-defined value and takes further actions based on comparison.

AVAILSIZE=$(df | head -n 2 | tail -n 1 | awk '{ print $4 }' | sed 's/G//')
if [[ $AVAILSIZE -lt 7.0 ]]
then
    echo "[INFO] Not enough disk space available. Proceeding with cleanup..."
else
    echo "[INFO] Enough disk space available. Skipping cleanup."
fi

I have written this code. But the problem here is, "AVAILSIZE" is a float number and executing above throws error "-bash: [[: 3.7: syntax error: invalid arithmetic operator (error token is ".7")".

I tried using bc command but it throws another error "-bash: bc: command not found".

Please help with this. Or may be suggest another approach doing what I intent to.

Edit 1:

This is my df command output. I am interested in value 3.7G from first Filesystem result. Variable AVAILSIZE will have 3.7 value in it.

Filesystem                                  Size  Used Avail Use% Mounted on
/dev/mapper/cl_p10--centos7--template-root   16G   13G  3.7G  78% /
devtmpfs                                    1.9G     0  1.9G   0% /dev
tmpfs                                       1.9G     0  1.9G   0% /dev/shm
tmpfs                                       1.9G  185M  1.7G  10% /run
Amit Jagtap
  • 121
  • 1
  • 1
  • 9

2 Answers2

1

How about awk all the way?:

$ df -H | awk '                                          # I need -H for floats
NR==2 {                                                  # the 2nd row of df
    if($4+0<7)                                           # 4.1G+0 = 4.1 
        print "[INFO] Not enough disk space available."  
    else 
        print "[INFO] Enough disk space available."
}'

Output:

[INFO] Not enough disk space available.

Instead of NR==1 you could define the mount point with $1=="/dev/sda1".

James Brown
  • 36,089
  • 7
  • 43
  • 59
0

the below worked. Thank you @Pacifist.

AVAILSIZE=$(df | head -n 2 | tail -n 1 | awk '{ print $4 }' | sed 's/G//')
if [[ $AVAILSIZE < 7.0 ]]
then
        echo "[INFO] Not enough disk space available. Proceeding with cleanup..."
else
        echo "[INFO] Enough disk space available. Skipping cleanup."
fi
Amit Jagtap
  • 121
  • 1
  • 1
  • 9