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") 
7
  • The \rs mean your file is in DOS/Windows format. Commented May 8, 2022 at 3: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. Commented May 8, 2022 at 4: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. Commented May 8, 2022 at 4:02
  • 1
    Consider making a habit of running your scripts through shellcheck.net before asking questions here. Commented May 8, 2022 at 4:05
  • You are missing the then after else if Commented May 8, 2022 at 4:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.