Crontab regular execution of bash footer error: [:-gt: unary operator expected

-sharp!/bin/sh  
filesize=`ls -l nohup.out | awk "{ print $5 }"`
echo "`date +%Y-%m-%d_%H:%M:%S` fileSize: $filesize bit" >> clearNohup.log
if [ $filesize -gt 1073741824 ]
then
    echo "`date +%Y-%m-%d_%H:%M:%S` $filesize bigger than 1G, we will back up and clear..." >> clearNohup.log
    cp -f nohup.out nohup.out.bak
    > nohup.out
    echo "`date +%Y-%m-%d_%H:%M:%S` file back up and claer over!" >> clearNohup.log
else
    echo "`date +%Y-%m-%d_%H:%M:%S` $filesize less than 1G,do nothing!" >> clearNohup.log
fi

the manual execution does not report an error, but it executes normally; it always reports an error during the crontab execution.
the system is centOS6
error:
clearNohup.sh: line 4: [:-gt: unary operator expected
ask for God"s advice!

Mar.03,2021

put this sentence:


:

-sharp filesize
filesize=

can be changed (based on -sharphammer bash env bash ):

  • filesize=$ {$(ls-l nohup.out | awk'{print $5}'):-0}
  • or add logic [- z "$filesize"] & & filesize=0

use other answers to remind ls-l nohup.out | awk'{print $5}' the ls command in front of the pipe may make an error. You can add set-eo pipefail and exit early

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46a1d-44bdb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46a1d-44bdb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?