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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not to execute any graph if the file in a directory is zero bytes
# 1  
Old 10-04-2010
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.
# 2  
Old 10-04-2010
Code:
if [ ! -z file ]; then
    blablabla
fi

# 3  
Old 10-04-2010
not to execute any grapf if the file in a directory is zero byte

Hi thanks for ur reply.but how to check for the latest file and execute that file if it is greater than zero bytes?
# 4  
Old 10-04-2010
Code:
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.
# 5  
Old 10-04-2010
Code:
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 ----------

Quote:
Originally Posted by teresaejunior
Code:
    if [ ! -z $file ]; then

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.
This User Gave Thanks to hergp For This Post:
# 6  
Old 10-04-2010
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:
Code:
ls --file-type -t | grep -v /$ | while read file; do
    if [ -s $file ]; then
        COMMAND YOU WANT TO RUN ON $file
        break
    fi
done

# 7  
Old 10-05-2010
this is the code which will execute without checking for the file is empty or not.

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Chroot: cannot execute /bin/sh: No such file or directory

Hi Expert, I'm having grub problem here: grub> root (hd0,0): Filesystem type is ext2 grub> setup Error 11 : Unrecognized Device String I try install boot loader follow the instruction from 26.2. Booting into Rescue Mode It says : select Continue, it attempts to mount your file... (4 Replies)
Discussion started by: justbow
4 Replies

2. Shell Programming and Scripting

Get file's first x bytes

is there a better way to do this: head -c 10000k /var/dump.log | head -c 6000k unfortunately, the "-c" option is not available on sun solaris. so i'm looking at "dd". but i dont know how to use it to achieve the same exact goal as the above head command. this needs to work on both solaris... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

4. UNIX for Dummies Questions & Answers

X bytes of 0, Y bytes of random data, Z bytes of 5, T bytes of 1. ??

Hello guys. I really hope someone will help me with this one.. So, I have to write this script who: - creates a file home/student/vmdisk of 10 mb - formats that file to ext3 - mounts that partition to /mnt/partition - creates a file /mnt/partition/data. In this file, there will... (1 Reply)
Discussion started by: razolo13
1 Replies

5. Programming

Copying 1024 bytes data in 3-bytes chunk

Hi, If I want to copy a 1024 byte data stream in to the target location in 3-bytes chunk, I guess I can use the following script. dd bs=1024 count=3 if=/src of=/dest But, I would like to know, how to do it via a C program. I have tried this with memcpy(), that did not help. (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Shell Programming and Scripting

Error PHP Fatal error: Allowed memory size of 67108864 bytes exhausted(tried to allocate 401 bytes)

While running script I am getting an error like Few lines in data are not being processed. After googling it I came to know that adding such line would give some memory to it ini_set("memory_limit","64M"); my input file size is 1 GB. Is that memory limit is based on RAM we have on... (1 Reply)
Discussion started by: elamurugu
1 Replies

7. Programming

how to inspect the bytes in a file?

What is the easiest way to inspect the bytes stored in a file? Ideally, If my file was 10 bytes each of which had only the high bit set, I'd be able to browse for it and get output like this: 01 - 10000000 02 - 10000000 03 - 10000000 04 - 10000000 05 - 10000000 06 - 10000000 07 -... (7 Replies)
Discussion started by: sneakyimp
7 Replies

8. UNIX for Dummies Questions & Answers

Bytes of character in file

Hi, How do I check for the total bytes of character used by a file? Can I used a od command to check? Thanks. (1 Reply)
Discussion started by: user50210
1 Replies

9. Shell Programming and Scripting

Remove first N bytes and last N bytes from a binary file on AIX.

Hi all, Does anybody know or guide me on how to remove the first N bytes and the last N bytes from a binary file? Is there any AWK or SED or any command that I can use to achieve this? Your help is greatly appreciated!! Best Regards, Naveen. (1 Reply)
Discussion started by: naveendronavall
1 Replies

10. UNIX for Dummies Questions & Answers

Take a file from the system and put on tape and reset the file to 0 bytes

:mad: I did this the other day but one of my support personnel removed my history so i could call it back up to remeber the exact command since i am air-headed at times. I am trying to take a 30 MEG file off the system and drop it to tape then i want to make the file go back to being 0 bytes so... (1 Reply)
Discussion started by: JackieRyan26
1 Replies
Login or Register to Ask a Question