viewing paste Unknown #48346 | Bash

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
LOGFILE="/var/log/addprinter2.log"
RETAIN_NUM_LINES=10
 
function logsetup {
    TMP=$(/usr/bin/tail -n $RETAIN_NUM_LINES $LOGFILE 2>/dev/null) && /bin/echo "${TMP}" > $LOGFILE
    exec > >(/usr/bin/tee -a $LOGFILE)
    exec 2>&1
}
 
function log {
    LOGTIME=`/bin/date "+%Y-%m-%d %H:%M:%S"`
    /bin/echo -e "[$LOGTIME]: $*"
}
 
logsetup
 
log starting script
log `env`
# get pi info:
piserial=$(/bin/cat /proc/cpuinfo | /bin/grep Serial | /usr/bin/cut -d ' ' -f 2)
log $piserial
host=$(/bin/cat /etc/default/autossh | /bin/grep :22 | /bin/grep ^tunnel | /usr/bin/cut -d '@' -f 2)
log $host
ipaddress=$(/sbin/ip route get 10.8.0.1 | /usr/bin/awk 'NR==1 {print $NF}')
log $ipaddress
 
function remove_all_printers {
    log removing all printers
    log `/usr/bin/lpstat -p | /usr/bin/cut -d" " -f2 | /usr/bin/xargs -I{} /usr/sbin/lpadmin -x {} 2>&1`
}
 
uris=( $(/usr/sbin/lpinfo -v | /bin/grep -Po '(?<=direct )usb:.+'| /usr/bin/sort) )
declare -a uris=('usb://ZebraOne')
 
log retriving uris...
for (( i = 0; i < ${#uris[@]}; i++ )); do
   log "${uris[$i]}"
done
 
# pass zebra's number
function set_up_printer {
    log `/bin/systemctl stop cups 2>&1`
    log `/usr/sbin/lpadmin -p Zebra"$1" -E -v "$2" 2>&1`
    log `/bin/echo -e "$1;\c" >> "/etc/cups/znumbers" 2>&1`
    log `/bin/echo "$uri" | /bin/grep -Po '(?<=serial\=).+' >> "/etc/cups/znumbers" 2>&1`
    log `/bin/systemctl restart cups 2>&1`
}
 
 
function get_unique_from_jazva {
    unique=$(/usr/bin/curl -s --data "piserial=$piserial&host=$host&ipaddress=$ipaddress&port=$1" http://www.jazva.com/jz/data/api/printer/provision)
    log unique number for $1 is $unique
}
 
remove_all_printers
# TODO: check if all printers removed
 
for (( i = 0; i < ${#uris[@]}; i++ )); do
    unique=""
    log setting up printer ${uris[$i]}
    set_up_printer $((i+1)) "${uris[$i]}"
    if [[ $((i+1)) = 1 ]]; then
        get_unique_from_jazva 9100
        if [ $unique =~ ^[A-Z0-9_]{4,50}$ ]; then
            /bin/echo "$unique" | /usr/bin/lp -d Zebra1
            # TODO: check lp result here
        else
            log wrong unique number for ${uris[$i]}
        fi
    fi
    if [[ $((i+1)) = 2 ]]; then
            get_unique_from_jazva 9101
            /bin/echo "$unique" | /usr/bin/lp -d Zebra2
    fi
    if [[ $((i+1)) = 3 ]]; then
            get_unique_from_jazva 9102
            /bin/echo "$unique" | /usr/bin/lp -d Zebra3
    fi
    if [[ $((i+1)) = 4 ]]; then
            get_unique_from_jazva 9103
            /bin/echo "$unique" | /usr/bin/lp -d Zebra4
    fi
done
Viewed 534 times, submitted by unknown.