0

I want to check the directory size:

• Low if the directory uses less than 3 MB of disk space

• Medium if the directory uses more than 3 MB and less than 8 MB of disk space

• High if the directory uses equal to or more than 8 MB disk space

For example:

> ./size.sh /usr/bin
High
> ./size.sh /tmp
Low

Here is the code that I try even though the folder contains this directory I still cannot do it.

#!/bin/bash

dir="breakfast"
DIRSIZE=`du -h "$dir"`
a=3145728
b=8388608
if (DIRSIZE -le "$a"); then 
    echo "Low"
else if (DIRSIZE -gt "$a" && DIRSIZE -lt "$b")
    echo "Medium"
else if (DIRSIZE -ge "$b")
    echo "High"
fi


Output:

sh quota.sh
: not found:
du: cannot access 'breakfast'$'\r': No such file or directory
quota.sh: 9: Syntax error: word unexpected (expecting "then")
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The `\r`s mean your file is in DOS/Windows format. – Charles Duffy May 08 '22 at 03:59
  • Beyond that, `-gt` is an argument to the `test` command, also called `[`. It doesn't make sense in another context -- `(` does not run `test`. – Charles Duffy May 08 '22 at 04:00
  • You also can't just use `DIRSIZE` and expect the variable name to be replaced with a value except in an arithmetic context, and if you _were_ in an arithmetic context then `-gt` wouldn't be legal syntax, you'd need to use `>` instead. – Charles Duffy May 08 '22 at 04:02
  • 1
    Consider making a habit of running your scripts through http://shellcheck.net/ before asking questions here. – Charles Duffy May 08 '22 at 04:05
  • You are missing the `then` after `else if` – mashuptwice May 08 '22 at 04:05
  • Another thing, though -- think about what dataformats things write. When you add the `-h` argument to `du`, for example, that makes it write things out in human units. Something written as a string in human units is not a number, so you can't _compare it to_ a number. – Charles Duffy May 08 '22 at 04:06
  • @mashuptwice, ...and _also_ the first `then` isn't recognized because it parses as `$'then\r'` on account of the trailing carriage return (because the script is saved as a DOS text file instead of a UNIX one). – Charles Duffy May 08 '22 at 04:06

0 Answers0