Not to execute any graph if the file in a directory is zero bytes

How to code shell script to know if the file in a dirctory is empty i.e., zero byte then not to execute any graph.Please help on this thanks in advance.

---------- Post updated at 08:18 AM ---------- Previous update was at 08:14 AM ----------

I know if clause wil help.my question is how to check the latest file in that dirctory.There will be many files in that dirctory in that we need to checjk for the latest file for zero byte,if it is zero byte not to execute any graph because what happening is when there is zero byte file our graph which are schedlued picks that zero byte file and fails,this should no happen.

if [ ! -z file ]; then
    blablabla
fi

Hi thanks for ur reply.but how to check for the latest file and execute that file if it is greater than zero bytes?

ls --file-type -t | grep -v /$ | while read file; do
    if [ ! -z $file ]; then
        COMMAND YOU WANT TO RUN ON $file
        break
    fi
done

Put double quotes around $file if it may contain spaces.

file=`ls -t | head -1`
if [ -s $file ]
then
    do_something_because_file_is_not_empty
fi

ls -t produces a list of filenames, sorted by modification time, newest first. head -1 picks the first filename (the newest).
This value is assign to the variable file.
The -s option in the test expression checks, if the file exists and is not of size zero.
If the test is true, something is executed.

---------- Post updated at 15:59 ---------- Previous update was at 15:57 ----------

Sorry to correct you, but -z tests, if the variable $file is empty and not if the file named by this variable is empty or not. Use -s for this.

1 Like

Yes, thanks, I didn't pay attention! But I think my loop ensures it is a file, not a directory. I mean, it will exit in your command if it is a directory. Maybe we have to know what he really needs.

This will make sure at least one file will be executed. With corrections:

ls --file-type -t | grep -v /$ | while read file; do
    if [ -s $file ]; then
        COMMAND YOU WANT TO RUN ON $file
        break
    fi
done

This is the code which are executing by using crontab schedulers without checking whether the file is empty or not,because fo this we are getting many alert mails when ever it is executing empty file i.e., zero byte file.So we need to write a condition like

If File is empty not execute the command
else execute the file with the command.

Note:Files are present in this path /amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/queue/SubmDist/subscribe.SubmAPCD

It should pick the latest file check for zero byte if not execute the command with the file which we checked above.

Code:
#!/bin/ksh
project3='/amex/abinitio/private_sand/GES/GlobalCapture/subm_I3'
subm=$project3
#echo $subm
export AB_HOME=/amex/abinitio/etl/abinitio-V2-14
export AB_AIR_ROOT=//148.173.245.169/amex/abinitio/reposit-V2-14/eme-V2-14
echo "Connected to EME 2-14"
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/mail-1.4.jar
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/activation-1.1.jar
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/
export CLASSPATH=$CLASSPATH:/amex/abinitio/private_sand/GES/GlobalCapture/subm_I3/class/
export PATH=$AB_HOME/bin:$PATH:/amex/abinitio/public_sand/stdenv/bin
export PATH=.:$AB_HOME/bin:/opt/IBMIHS/IBMJava2-142/bin:$PATH
envir=$subm
db2name="/usr/local/db2/db2clnt2/sqllib/db2profile"
. $envir/ab_project_setup.ksh $envir
## For accessing databse from unix linux servers
if [ -f /usr/local/db2/db2clnt1/sqllib/db2profile ]
then
. /usr/local/db2/db2clnt1/sqllib/db2profile
elif [ `uname -s` = "Linux" ]
then
if [ `uname -a | awk '{ print $2 }'` = "lpdma512" ]
then
. /db2/db2clnt1/sqllib/db2profile
elif [ `uname -a | awk '{ print $2 }'` = "lpdwa723" ]
then
. /usr/local/db2/db2clnt2/sqllib/db2profile
elif [ `uname -a | awk '{ print $2 }'` = "lpqma515" ]
then
. /usr/local/db2/db2clnt1/sqllib/db2profile
fi
fi

# ---- GLOBAL VARIABLE ---------#
export ProcessStartTS=`date '+%Y-%m-%d-%H.%M.%S'`
export LogName=$AI_SERIAL_LOG/APCDAutomation_${ProcessStartTS}.log
# export FileCount=`ls -lrt $AI_SERIAL_INBOX | tail -1 | egrep -v '\.SC|\.SP1|\.SP2' | wc -l`

#------------GRAPH FUNCTIONS ---------- #
function run
{
GraphName=$1
print "Processing $GraphName on $(date)" >> $LogName
$AI_BIN/./$GraphName >> $LogName
if [[ $? -ne 0 ]] then
print "$GraphName Failed " >> $LogName
EmailFailedMsg $GraphName
BackUpFailedGraph $GraphName
exit 3
fi
}
function EmailFailedMsg
{
Graph=$1
`java -classpath $CLASSPATH sendmailJobFail alert krb@yahoo.com abc@yahoo.com $Graph`
}

function BackUpFailedGraph
{
GraphNm=$1
case $GraphNm in
"SubDisA.ksh" ) cp $AI_SERIAL/ListOfAPCDFiles.dat $AI_SERIAL/OPbackup/ListOfAPCDFiles-${ProcessStartTS}.dat;;
esac
}
function CleanUp
{
cp -f /dev/null $AI_SERIAL_LOOKUP/Sha1FileHistoryLookup.dat
}

# ------MAIN FUNCTION------------------ #
print " Running files for One Point Automation on $(date) " >> $LogName
# if [ $FileCount = 0 ]
# then
# echo "No file is not in inbox"
# exit 2
# fi
run SubDisA.ksh
# CleanUp # clear the sha1 and the input file

---------- Post updated at 10:01 AM ---------- Previous update was at 04:27 AM ----------

This is the code which are executing by using crontab schedulers without checking whether the file is empty or not,because fo this we are getting many alert mails when ever it is executing empty file i.e., zero byte file.So we need to write a condition like

If File is empty not execute the command
else execute the file with the command.

Note:Files are present in this path /amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/queue/SubmDist/subscribe.SubmAPCD

It should pick the latest file check for zero byte if not execute the command with the file which we checked above.

Code:
#!/bin/ksh
project3='/amex/abinitio/private_sand/GES/GlobalCapture/subm_I3'
subm=$project3
#echo $subm
export AB_HOME=/amex/abinitio/etl/abinitio-V2-14
export AB_AIR_ROOT=//148.173.245.169/amex/abinitio/reposit-V2-14/eme-V2-14
echo "Connected to EME 2-14"
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/mail-1.4.jar
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/activation-1.1.jar
export CLASSPATH=$CLASSPATH:/amex/abinitio/data_mount/GES/GlobalCapture/subm_I3/serial/Jars/
export CLASSPATH=$CLASSPATH:/amex/abinitio/private_sand/GES/GlobalCapture/subm_I3/class/
export PATH=$AB_HOME/bin:$PATH:/amex/abinitio/public_sand/stdenv/bin
export PATH=.:$AB_HOME/bin:/opt/IBMIHS/IBMJava2-142/bin:$PATH
envir=$subm
db2name="/usr/local/db2/db2clnt2/sqllib/db2profile"
. $envir/ab_project_setup.ksh $envir
## For accessing databse from unix linux servers
if [ -f /usr/local/db2/db2clnt1/sqllib/db2profile ]
then
. /usr/local/db2/db2clnt1/sqllib/db2profile
elif [ `uname -s` = "Linux" ]
then
if [ `uname -a | awk '{ print $2 }'` = "lpdma512" ]
then
. /db2/db2clnt1/sqllib/db2profile
elif [ `uname -a | awk '{ print $2 }'` = "lpdwa723" ]
then
. /usr/local/db2/db2clnt2/sqllib/db2profile
elif [ `uname -a | awk '{ print $2 }'` = "lpqma515" ]
then
. /usr/local/db2/db2clnt1/sqllib/db2profile
fi
fi

# ---- GLOBAL VARIABLE ---------#
export ProcessStartTS=`date '+%Y-%m-%d-%H.%M.%S'`
export LogName=$AI_SERIAL_LOG/APCDAutomation_${ProcessStartTS}.log
# export FileCount=`ls -lrt $AI_SERIAL_INBOX | tail -1 | egrep -v '\.SC|\.SP1|\.SP2' | wc -l`

#------------GRAPH FUNCTIONS ---------- #
function run
{
GraphName=$1
print "Processing $GraphName on $(date)" >> $LogName
$AI_BIN/./$GraphName >> $LogName
if [[ $? -ne 0 ]] then
print "$GraphName Failed " >> $LogName
EmailFailedMsg $GraphName
BackUpFailedGraph $GraphName
exit 3
fi
}
function EmailFailedMsg
{
Graph=$1
`java -classpath $CLASSPATH sendmailJobFail alert krb@yahoo.com abc@yahoo.com $Graph`
}

function BackUpFailedGraph
{
GraphNm=$1
case $GraphNm in
"SubDisA.ksh" ) cp $AI_SERIAL/ListOfAPCDFiles.dat $AI_SERIAL/OPbackup/ListOfAPCDFiles-${ProcessStartTS}.dat;;
esac
}
function CleanUp
{
cp -f /dev/null $AI_SERIAL_LOOKUP/Sha1FileHistoryLookup.dat
}

# ------MAIN FUNCTION------------------ #
print " Running files for One Point Automation on $(date) " >> $LogName
# if [ $FileCount = 0 ]
# then
# echo "No file is not in inbox"
# exit 2
# fi
run SubDisA.ksh
# CleanUp # clear the sha1 and the input file

First we need to check for the latest file in the directory & check If the file is Zero byte not execute else execute the graph by taking this file.:confused: