Script to read file size and send email only if size > 0.

Hi Experts,
I have a script like

 
$ORACLE_HOME/bin/sqlplus username/password # << ENDSQL
set pagesize 0 trim on feedback off verify off echo off newp none timing off
set serveroutput on
set heading off
spool Schemaerrtmp.txt
select ' TIMESTAMP                     COMPUTER NAME                        ERROR TEXT ' from dual;
select to_char(error_time,'MM-DD-YYYY.HH.MI.SS') ||'  ;  ' || trim(computer_name) || '  ;  '||trim(error_code)|| ' ; '|| trim(rpad(error_text,30)) from  ta_error_log where rownum <=1000 and error_time > sysdate -1;
spool off
ENDSQL
dt=`date|awk '{print $1}'`
sed '1,5 d' Schemaerrtmp.txt > Schemaerr.txt
cat Schemaerr.txt |mailx -s " SPEAR Error Log file!!!! " $MAIL_LIST

This script sends a message even if "Schemaerr.txt" file is empty/zero bytes.
Need to modify the above script so that it send message only if "Schemaerr.txt" size is > 0 or non-empty.

Please advice.

Thanks

This old man test option is popular for if empty('/bin/[' is actually an link to /bin/test, for shells where it is not built in!)

if [ ! -s $msg_file ]
then
 . . .
fi

Since the "-s" option of the test command tests "FILE exists and has a size greater than zero"

The answer is not "! -s" but rather "-s" to satisfy the OPs needs.

if [ -s Schemaerr.txt ] 
then 
    cat Schemaerr.txt | mailx -s " SPEAR Error Log File!!!! " $MAIL_LIST
fi

or you can abbreviate this as follows:

[ -s Schemaerr.txt ] && cat Schemaerr.txt | mailx -s " SPEAR Error Log File!!!! " $MAIL_LIST

or can eliminate the UUOC:

[ -s Schemaerr.txt ] && mailx -s " SPEAR Error Log File!!!! " $MAIL_LIST < Schemaerr.txt

Great point, cat *is useless here.
I totally missed the preferred redirect solution on that one.

I call it cut-trivial-cases-off-early-to-keep-things-simple+together+not-overly-indented logic, hate logic statements barefoot in flow, and like logging in unattended processes:

if [ ! -s $msg ]
then
 date "+%Y-%m-%d %H:%M:%S ${0##*/} ($$) Discarding empty message '$msg'." >&2
 continue ?  exit ?
fi