0

我正在尝试在我的树莓派上创建打印服务。这个想法是为打印作业创建一个 pop3 帐户,我可以在其中发送 PDF 文件并在家中打印它们。因此我设置了fetchmail & rarr; procmail & rarr; uudeview 收集电子邮件(使用白名单),提取文档并将其保存到/home/pi/attachments/. 到目前为止,一切正常。

为了打印文件,我想设置一个 shell 脚本,我计划每分钟通过一个 cronjob 执行该脚本。这就是我现在卡住的地方,因为我收到“权限被拒绝”消息,并且脚本在手动执行命令时工作时根本没有打印任何内容。

这是我的脚本的样子:

#!/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

脚本执行后,我得到以下反馈:

1 message for print@MYDOMAIN.TLD 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 print@MYDOMAIN.TLD@SERVER.HOSTER.TLD:1 of 1 (32139 octets) flushed
mail2print.sh: 6: mail2print.sh: /home/pi/attachments/Stage2.pdf: Permission denied

电子邮件从 pop3 帐户中提取,附件被提取并在其中出现片刻~/attachements/,然后被删除。但是没有打印输出。

任何想法我做错了什么?

4

2 回答 2

2
if  $f == "*.pdf"

应该

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

我也觉得

FILES=/home/pi/attachments/*

应该引用:

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

建议:

#!/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
于 2014-07-09T20:58:52.597 回答
-1

首先使用下面过滤pdf文件,然后您可以在for循环中删除该if语句。

FILES="ls /home/pi/attachments/*.pdf" 
于 2014-07-09T21:21:04.037 回答