Directory checking bash script

Posted on 2012-09-19
Last Modified: 2013-11-13
I have a small part of a bash script I am working on that needs to check for the number of a specific file type (*.spl) in a directory. If the files detected are zero (0), then the echo message needs to write to a logfile the message why and then break / exit thus stopping the script.

So far the following isn't working as expected:
# Count *.spl files found to process
for file in $( ls -1 $dump_dir/*.spl 2>/dev/null )
  file_count=`expr $file_count + 1`
  if [ $retval -ne 0 ]
    echo -e "   Aborting file transfer (No SPL files found) at `date "+%D %k:%M:%S"`" >> $logfile

echo -e "   Found $file_count files to process at `date "+%D %k:%M:%S"`" >> $logfile


Open in new window

It keeps showing the same thing via 'set -x' output:
+ file_count=0
++ ls -1 '/home/tims/transfer/*.spl'
++ date '+%D %k:%M:%S'
+ echo -e '   Found 0 files to process at 09/19/12 13:23:40'
+ exit

Open in new window

In the log file it is showing this:
Starting Daily TIMS Transfer at 09/19/12 13:23:40
   Found 0 files to process at 09/19/12 13:23:40

Open in new window

The intent is that if no files are found, the script needs to fully stop.
But it keeps the script going, attempting to zip up zero files, etc.
Question by:Michael W
    LVL 68

    Accepted Solution

    In line 11 replace "break" with "exit".

    And shouldn't it read in line 7 "-eq 0" ?
    LVL 29

    Author Closing Comment

    by:Michael W
    That worked like a charm.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now