0

我有一个下面的 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
4

1 回答 1

0

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

PRINTERLINE=$(grep 'PRINTER=' $MAILFILE)
eval $PRINTERLINE #eval PRINTER=FOO
#$PRINTER equals to FOO now
于 2013-04-20T15:29:33.450 回答