Reputation: 385
I am currently running a script (check_files.sh) that may or may not produce match* files (eg. match1.txt, match2.txt, etc. I then want to go on to run an R script on the produced match* files.
However, before I run the R script, I have tried a line in my script which checks if the files are present.
if [ -s match* ]
then
for f in match*
do
Rscript --vanilla format_matches.R ${f}
rm match*
done
else
echo "No matches present"
fi
However, I keep getting an error message (as there are often a lot of match files producted):
./check_files.sh: line 52: [: too many arguments
Is there an alternative to [ -s match* ] which would not throw up the error message? I take it that the error message appears as there are multiple match* files produced.
Upvotes: 0
Views: 100
Reputation: 4875
If you expect filenames with spaces, this is a bit ugly but robust:
found=0
find -maxdepth 1 -type f -name 'match*' -print0 | while IFS= read -rd $'\0' f
do
found=1
Rscript --vanilla format_matches.R "$f"
rm "$f"
done
if [ "$found" -eq 0 ]; then
>&2 echo "No matches present"
fi
Upvotes: 2
Reputation: 249
Is there an alternative to [ -s match* ] which would not throw up the error message?
The following works for my example file match1.txt
if [ -f ~/match* ]; then
echo "yeha"
fi
Upvotes: 0
Reputation: 47137
You could change your logic to the following:
found=0
for f in match*; do
[ -e "$f" ] || continue
found=1
Rscript --vanilla format_matches.R "$f"
rm "$f"
done
if [ "$found" -eq 0 ]; then
>&2 echo "No matches present"
fi
Upvotes: 2