[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

Directory checking bash script

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.
Michael Worsham
Michael Worsham
1 Solution
In line 11 replace "break" with "exit".

And shouldn't it read in line 7 "-eq 0" ?
Michael WorshamInfrastructure / Solutions ArchitectAuthor Commented:
That worked like a charm.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now