#!/bin/bash ## Author Luis@32768.org at 2012-02-14 ## Check HP LaserJet 1020 is plugged in or not every 6 seconds ## If pluged, make a 1 flag to /tmp/hplj1020 ## If unpluged, remove /tmp/hplj1020 ## If not pluged, do nothing CHECKFILE=/proc/bus/usb/devices FLAGCHECK='HP LaserJet 1020' FLAGFILE=/tmp/hplj1020 FIRMWARE=/root/sihp1020.dl PRINTER=/dev/usb/lp0 DEBUG=0 rm -rf $FLAGFILE while(true) do printeronline=0 flagfileexist=0 firmwareloaded=0 ## check printer is online or not pscnt=`cat ${CHECKFILE} | grep "${FLAGCHECK}" | wc -l` if [ $pscnt -gt 0 ] then ## HP LaserJet 1020 is online printeronline=1; ## DEBUG if [ $DEBUG -eq 1 ] then echo "Printer is online" fi else ## DEBUG if [ $DEBUG -eq 1 ] then echo "Printer is NOT online" fi fi ## check flag file if [ -f $FLAGFILE ] then ## flag file is exist flagfileexist=1; ## DEBUG if [ $DEBUG -eq 1 ] then echo "Flag file exist" fi ## check the content firmwareloaded=`cat ${FLAGFILE}` ## DEBUG if [ $DEBUG -eq 1 ] then echo "Firmware loaded: $firmwareloaded" fi else ## DEBUG if [ $DEBUG -eq 1 ] then echo "Flag file NOT exist" fi fi ## now we get all three flag ## No priniter, clear flag file if [ $printeronline -eq 0 ] then if [ -f $FLAGFILE ] then rm -rf $FLAGFILE ## DEBUG if [ $DEBUG -eq 1 ] then echo "Remove flag file" fi fi fi ## Check printer online or NOT if [ $printeronline -eq 1 ] then ## Printer exist, load firmware if NOT load if [ $firmwareloaded -lt 1 ] then ## Load firmware first cat $FIRMWARE > $PRINTER ## DEBUG if [ $DEBUG -eq 1 ] then echo "Load firmware to printer" fi ## Flag it if [ $flagfileexist -eq 1 ] then ## Flag file exist, flag it echo "1" > $FLAGFILE ## DEBUG if [ $DEBUG -eq 1 ] then echo "Flag the exist of printer" fi else ## Flag file NOT exist, touch it and flag it touch $FLAGFILE echo "1" > $FLAGFILE ## DEBUG if [ $DEBUG -eq 1 ] then echo "Create flag file and flag it" fi fi fi fi sleep 2; done