Vishnu Vishnu - 10 days ago 4x
Bash Question

Running a script to check disk usage creates a new file. why?

When I excecute the file which has content as follows

USE=`df -h "/scratch/" | tail -1 | awk '{ print $4 }' | sed s/%//g`
if [ $USE>30 ]; then
echo "The limit has exceeded"
echo "The limit is not exceeded"

It creates a new file 40 in the directory where it is executed. Why is this happening?


The problem is this line:

if [ $USE>30 ]; then

>30 means to redirect the output of the command to a file named 30 (not 40). You need to quote the > character to prevent its special meaning. Also, the test command requires each argument to be separated:

if [ $USE '>' 30 ]; then

However, > is for alphanumeric comparison. For numeric comparisons you need to use `-gt:

if [ $USE -gt 30 ]; then