Search code examples
bashshellpermission-deniedprocmail

Printing shell script doesn't print anything


I'm trying to create a print service von my raspberry pi. The idea is to have a pop3 account for print jobs where I can sent PDF files and get them printed at home. Therefore I set up fetchmail & rarr; procmail & rarr; uudeview to collect the emails (using a whitelist), extract the documents and save them to /home/pi/attachments/. Up to this point everything is working.

To get the files printed I wanted to set up a shell script which I planned to execute via a cronjob every minute. That's where I'm stuck now since I get "permission denied" messages and nothing gets printed at all with the script while it works when executing the commands manually.

This is what my script looks like:

#!/bin/bash
fetchmail                 # gets the emails, extracts the PDFs to ~/attachments
wait $!                   # takes some time so I have to wait for it to finish
FILES=/home/pi/attachments/*
for f in $FILES; do       # go through all files in the directory
   if  $f == "*.pdf"      # print them if they're PDFs
   then
      lpr -P ColorLaserJet1525 $f
   fi
   sudo rm $f             # delete the files
done;
sudo rm /var/mail/pi      # delete emails

After the script is executed I get the following Feedback:

1 message for [email protected] at pop3.MYDOMAIN.TLD (32139 octets).
Loaded from /tmp/uudk7XsG: 'Test 2' (Test): Stage2.pdf part 1   Base64
Opened file /tmp/uudk7XsG
procmail: Lock failure on "/var/mail/pi.lock"
reading message [email protected]@SERVER.HOSTER.TLD:1 of 1 (32139 octets) flushed
mail2print.sh: 6: mail2print.sh: /home/pi/attachments/Stage2.pdf: Permission denied

The email is fetched from the pop3 account, the attachement is extracted and appears for a short moment in ~/attachements/ and then gets deleted. But there's no printout.

Any ideas what I'm doing wrong?


Solution

  • if  $f == "*.pdf"
    

    should be

    if  [[ $f == *.pdf ]]
    

    Also I think

    FILES=/home/pi/attachments/*
    

    should be quoted:

    FILES='/home/pi/attachments/*'
    

    Suggestion:

    #!/bin/bash
    fetchmail                      # gets the emails, extracts the PDFs to ~/attachments
    wait "$!"                      # takes some time so I have to wait for it to finish
    shopt -s nullglob              # don't present pattern if no files are matched
    FILES=(/home/pi/attachments/*)
    for f in "${FILES[@]}"; do                              # go through all files in the directory
        [[ $f == *.pdf ]] && lpr -P ColorLaserJet1525 "$f"  # print them if they're PDFs
    done
    sudo rm -- "${FILES[@]}" /var/mail/pi         # delete files and emails at once