2

I am attempting to create a cronjob that will run depending on the output of disk usage for a specific folder. In my test environment, I have a folder called "Test" that is 4.0 Kilobytes. I am querying its size with:

du -csh /home/<username>/Test | grep total | cut -f1 | tr -d 'K'

The output is assigned to a variable called DISK_SPACE and leaves me with 4.0. I cannot figure out how to convert 4.0 to an integer in order to satisfy the following:

if [ $DISK_SPACE -gt 3.0 ]
then
rm -rf /home/<username>/Test/*.*
else
echo $DISK_SPACE "is less than 3.0K and as a result the contents of the folder will NOT be deleted."

My full bash file looks like the below:

#!/bin/bash
DISK_SPACE=$(du -csh /home/<username>/Test | grep total | cut -f1 | tr -d 'K')
echo $DISK_SPACE
if [ $DISK_SPACE -gt 3.0 ]
then
rm -rf /home/<username>Test/*.*
else
echo $DISK_SPACE "is less than 3.0K and as a result the contents of the folder will NOT be deleted."
fi

The error I receive after running this is:

4.0: integer expression expected
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Your title suggests a more general question than just one involving disk utilization computation and comparison. – JRFerguson Mar 19 '19 at 19:17

4 Answers4

4

du -h prints numbers for human consumption. A script shouldn't use -h. Try -k or -b instead to get easy-to-parse integers:

-k     like --block-size=1K

-b, --bytes
       equivalent to '--apparent-size --block-size=1'
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Use printf to round your real number to an integer.

printf "%1.0f\n" 4.0
4

printf "%1.0f\n" 4.6
5

See the printf manpages.

For you example you can do:

DISK_SPACE=4.0
DISK_SPACE=$(printf "%1.0f" ${DISK_SPACE})
echo ${DISK_SPACE}
4
JRFerguson
  • 7,426
  • 2
  • 32
  • 36
0

Bash has no support for floating point arithmetic. If you just care whether the first digit is bigger than 3, you can simply trim any decimals.

if [ "${disk_space%.*}" -gt 3 ]; then ...

If you need proper float comparison, perhaps it's simpler to actually use a tool which supports floating-point arithmetic.

Notice also that I converted your variable to lower case. You should not use all-uppercase names for your private variables.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Your second example is missing a closing `"` on the variable expansion. And it's also broken in that it dies with an error for `disk_space=3.0`. – Mike Holt Mar 19 '19 at 19:07
  • Thanks, removed that portion, which was kind of silly anyway. – tripleee Mar 19 '19 at 19:14
  • Yeah, it was probably overkill. But it actually would have worked if you'd replaced the `-eq` with `=` (i.e., just doing regular string comparison instead of a numeric "equals" test). In that case, things like `"3.0000"`, etc would just stay as they were and therefore fail the string comparison as intended, while things like `"3.00195"` would be chopped down to `"3"`. – Mike Holt Mar 19 '19 at 19:17
0

In addition to John Kugelman's answer I also changed the following line:

Original:

if [ $DISK_SPACE -gt 3.0 ]

New:

if [ $DISK_SPACE -gt 3072 ]

This allowed John's recommended change to work even better with the script:

du -csb /home/<username>/Test | grep total | cut -f1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578