Vishnu Vishnu - 1 month ago 7
Bash Question

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

When I excecute the script.sh 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"
else
echo "The limit is not exceeded"
fi


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

Answer

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