LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Newbie troubles with Bash if/then statements (http://www.linuxquestions.org/questions/programming-9/newbie-troubles-with-bash-if-then-statements-122644/)

jimieee 12-04-2003 09:23 AM

Newbie troubles with Bash if/then statements
 
Hi I'm trying to write a bash script to convert incoming faxes every hour and have managed to get it down ok, apart from and if/then statement:

#!/bin/bash
FAXES="/var/spool/fax/incoming/*.01"
check=0
nofaxes=0

for fax in $FAXES
do
check=$(($check+1))
done

if [ $check > 1 ] > /dev/null
then
for fax in $FAXES
do
nofaxes=$(($nofaxes+1))
cat $fax | /usr/bin/g32pbm > "$fax".tif
mv "$fax".tif /home/samba/faxesin/
rm -f "$fax"
done
echo $nofaxes " faxes processed"
fi


When I run the script I get

cat: /var/spool/fax/incoming/*.01: No such file or directory
1 faxes processed


Why doesn't the script realise that the directory is empty? Do I need some kind of "if not null" statement instead of trying to count the number of faxes? If so how do I do this?

Regards,

James

ilikejam 12-04-2003 09:58 AM

Hi.

Try:

if cat ....... then
<deal with faxes>
else
<don't deal with faxes>
fi

the "if cat" will return false (if I remember correctly) if it encounters an error. Just don't use "if [cat.....]

Dave

jim mcnamara 12-04-2003 10:00 AM

You can check for existence first, I removed a lot of your file processing:
Code:

#!/bin/sh
FAXES="/home/wherever/*.01"
check=0
nofaxes=0
ls $FAXES > /dev/null 2>/dev/null
if [ $? -gt 0 ]; then
  echo "No files found"
  exit
fi
for fax in $FAXES
do
  nofaxes=$(($nofaxes+1))
done
echo "$nofaxes  faxes processed"


jimieee 12-04-2003 12:08 PM

Thanks guys, I went for the second method in the end (works great, thanks!) because I wasn't sure about the first. Would I have included the ....... in the cat statement?

Thanks,

James

ilikejam 12-04-2003 06:33 PM

No, I just meant to continue with the rest of the line. Glad you got it working........ ;-)


All times are GMT -5. The time now is 01:35 PM.