Thursday, February 22, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  9] [ 0]  / answers: 1 / hits: 5278  / 2 Years ago, tue, october 12, 2021, 3:28:47

Ubuntu 14.04.1.

I have a bash script, called by cron every 10 minutes, which basically looks for files in a subdir, then loops through and process each file. But how do I check if there are no files found? If there are no files found I don't want to process them, and I don't want to get an email via cron that says "no files found", because cron runs every 10 minutes. That's 144 emails per day saying "no files found" that I don't want to get.

  • The input/ dir is owned by me and has full rwx permissions.

  • I've already guaranteed the files in input/ do not contain spaces, thanks to another answer on Ask Ubuntu.

Here's my basic script.

# Cron requires a full path in $myfullpath
files=`ls $myfullpath/input/fedex*.xlsx`
# How do I check for no files found here and exit without generating a cron email?
for fullfile in $files


Thanks! I didn't even know what to google for this one.

EDIT: My script is now this:

# Script: gocronloop, Feb 5, 2015
# Cron requires a full path to the file.
cd $mydir/input
# First remove spaces from filesnames in input/
find -name "* *" -type f | rename 's/ /-/g'
cd $mydir
shopt -s nullglob
if [ $? -ne 0 ]; then
echo "ERROR in shopt"
exit 1
for fullfile in "$mydir"/input/fedex*.xlsx
# First remove file extension to get full path and base filename.
myfile=`echo "$fullfile"|cut -d'.' -f1`
echo -e "
Doing $myfile..."
# Convert file from xlsx to xls.
ssconvert $myfile.xlsx $myfile.xls
# Now check status in $?
if [ $? -ne 0 ]; then
echo "ERROR in ssconvert"
exit 1
perl $1 $mydir/ -input:$mydir/$myfile.xls -progdir:$mydir

More From » bash


First things first: Don't parse ls.

Now that we have got that out of the way, use globbing, alongwith nullglob:

shopt -s nullglob
for fullfile in "$myfullpath"/input/fedex*.xlsx

Usually with globbing, if * doesn't match anything it's left as is. With nullglob, it is replaced with nothing, so a false match isn't triggered.

For example:

$ bash -c 'a=(foo/*); echo ${a[@]}'
$ bash -c 'shopt -s nullglob; a=(foo/*); echo ${a[@]}'


[#21423] Thursday, October 14, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 421
Total Questions: 92
Total Answers: 109

Location: Uzbekistan
Member since Mon, Jul 20, 2020
4 Years ago