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
1
  • Your title suggests a more general question than just one involving disk utilization computation and comparison. Commented Mar 19, 2019 at 19:17

4 Answers 4

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'
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Under bash use printf -v DISK_SPACE %.0f $DISKSPACE. This will avoid one fork and use builtin printf -v to set variable.
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.

3 Comments

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.
Thanks, removed that portion, which was kind of silly anyway.
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".
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.