Carl Sagan
Carl Sagan

Reputation: 982

bash: how do I parse the results of one command to use as match for another command

The intention of my script is to look for the usb printer and make sure the properties file is populated according to what's connected.

What I'm currently doing is:

ls -l /dev/usb | grep 'lp'

returns something like:

crw-rw---- 1 root lp   180, 1 Aug  5 11:32 lp1
crw-rw---- 1 root lp   180, 2 Aug  5 11:32 lp2

which I'd like to take the lp1 and lp2 values, and check my pos.properties file:

machine.printer=epson\:file,/dev/usb/lp2
machine.printer.3=epson\:file,/dev/usb/lp4
machine.printer.2=epson\:file,/dev/usb/lp1

for the strings '/dev/usb/lp1', '/dev/usb/lp2'. If both match, output 'OK, otherwise, output missing printer(s)

Upvotes: 1

Views: 71

Answers (2)

chepner
chepner

Reputation: 531055

ok=1
for printer in /dev/usb/lp*; do
    if ! grep -wq "$printer" pos.properties; then
        ok=0
        echo "Missing $printer"
        break
    fi
done
(( ok )) && echo "OK"

Depending on the format of pos.properties, you may need a more specific command, for example,

if ! grep -wq "$printer" <(cut -d, -f2 pos.properties); then

Upvotes: 2

anubhava
anubhava

Reputation: 785058

You can use this script:

cd /dev/usb/
arr=(lp*)

[[ $(grep -cFf <(printf "/dev/usb/%s\n" "${arr[@]}") pos.properties) -eq ${#arr[@]} ]] && 
    echo "OK" || echo "missing printer(s)"

Upvotes: 1

Related Questions