2013-04-03 127 views
0

我有一個下面的shell腳本,用於檢查電子郵件帳戶並自動將附件打印到腳本中第8行列出的打印機上。你能否幫我修改它,以便第8行是一個變量,並且可以通過在電子郵件正文中插入適當的文本PRINTER = FOO來設置變量。這將允許腳本打印到多臺打印機。用於在多臺打印機中打印的Shell腳本

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

回答

0

如果限制打印機= FOO到自己的路線,那將是很容易:

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