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

.
Menu