用于在多台打印机中打印的Shell脚本

时间:2013-04-03 08:37:01

标签: shell

我有一个下面的shell脚本,用于检查电子邮件帐户并自动将附件打印到脚本中第8行列出的打印机上。你可以帮我修改它,这样第8行就是一个变量,可以通过在电子邮件正文中插入适当的文本PRINTER = FOO来设置变量。这将允许脚本打印到多台打印机。

#!/bin/bash
#while [ 1 ]
while [ 1 ]
do
SUPPORTED_FILETYPES=".pdf"
#LP_OPTIONS="-o media=A4,tray1 -o fit-to-page -o position=top -o scaling=100"
LP_OPTIONS=""
PRINTER="PRT04-3" #line no. 8 

MAILFILE=~/eprint/$(date +%H%M%S).txt

PRINT_FOLDER=~/eprint/printable
/usr/bin/fetchmail --bsmtp $MAILFILE

if [ "$?" = "0" ]; then
   MAIL_ADDRESS=$(grep 'From:' $MAILFILE | sed -n -e 's/^[^<]*<\([^>]*\)>.*$/\1/p')
   /usr/bin/uudeview +e $SUPPORTED_FILETYPES -p $PRINT_FOLDER -i $MAILFILE
   rm $MAILFILE

   PRINTED="no"

   for f in $PRINT_FOLDER/*

   do
      if [ "$f" != "$PRINT_FOLDER/*" ]; then
         LP_OUTPUT=$(lp $LP_OPTIONS "$f" -d $PRINTER)
         if [ "$?" != "0" ]; then
            MAILTEXT="File "$f" could not be printed."
            echo "$MAILTEXT" | mail -s "Print-Error" $MAIL_ADDRESS
         fi
         rm "$f"
         PRINTED="yes"
      else
         if [ "$PRINTED" = "no" ]; then
            echo "No printable Attachments" | mail -s "Print-Error" $MAIL_ADDRESS
         fi
      fi
   done
  # ~/mailprint
fi
sleep 4
done

1 个答案:

答案 0 :(得分:0)

如果您将PRINTER = FOO限制为自己的行,那将很容易:

PRINTERLINE=$(grep 'PRINTER=' $MAILFILE)
eval $PRINTERLINE #eval PRINTER=FOO
#$PRINTER equals to FOO now