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
  • 111
  • 1
  • 1
  • 9

3 Answers3

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
  • 34,397
  • 6
  • 36
  • 56
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
  • 111
  • 1
  • 1
  • 9
0

How I do this kind of test:

{ read foo;read foo foo foo avail foo;}< <(df -k /)
if ((avail < 7*1024**2)) ;then
    echo "[INFO] Not enough disk space available. Proceeding with cleanup..."
else
    echo "[INFO] Enough disk space available. Skipping cleanup."
fi

Explanation:

  1. cmd1 < <(cmd2) Run cmd1 with output of cmd2 as input.
  2. { read foo; read foo foo foo avail foo;}< <(...) will drop 1st line, then store 4th elem of 2nd line in $avail (and rest of line in $foo)
  3. if (()) ;then for numerical tests
  4. (( a**b )) a power b, so 7*1024**2 Kb => 7Gb.

From there, if you really want, you could output pseudo float:

txtsize() {
    local _c=$1 _i=0 _a=(K M G T P) _n=$'\n'
    while [ ${#_c} -gt 3 ] ;do
        (( _i++, _c>>=10 ))
      done
    _c=000$(( ( $1*1000 ) >> ( 10*_i ) ))
    ((_i+=${3:-0}))
    printf ${2+-v} $2 "%.2f%s${_n[${2+2}]}" \
        ${_c:0:${#_c}-3}.${_c:${#_c}-3} ${_a[_i]}
}

{ read foo;read foo foo foo avail foo;}< <(df -k /)
txtsize $avail humanavail
if ((avail < 7*1024**2)) ;then
    echo "[INFO] Not enough disk space available: $humanavail. Cleanup..."
else
    echo "[INFO] Enough disk space available: $humanavail. Skipping cleanup."
fi

This work without bc and without forks.

F. Hauri
  • 58,205
  • 15
  • 105
  • 122