I am running a grep command and getting the output below:

user@hostname:~$ ps -o args | grep lp_ | grep -v grep | sort -n
/usr/local/bin/lp_server -n 5964 -d /dev/usb/lp1
/usr/local/bin/lp_server -n 5965 -d /dev/usb/lp0
{lp_supervisor} /bin/sh /usr/local/lp/lp_supervisor /dev/usb/lp0 SAMPLESERIAL1 /var/run/lp/lp_pid/usb_lp0
{lp_supervisor} /bin/sh /usr/local/lp/lp_supervisor /dev/usb/lp1 SAMPLESERIAL2 /var/run/lp/lp_pid/usb_lp1

The end goal is to have SAMPLESERIAL1 (3rd line) assigned with port 5964 and SAMPLESERIAL2 (4th line) assigned with port 5965 (there are time when these ports get flipped as you can see above)

I need to check which /dev/usb/lp# is assigned to which port.

Then check which /dev/usb/lp# is assigned to which SAMPLESERIAL#

Eventually I'll have something like:

if SAMPLESERIAL1 is set to 5964:
    do nothing
    run command to change

I'm not sure the best way to go about this. I'm not too experienced with bash. My original thought was to try and parse the output into variables, but there may be a more efficient way to do this. I'm running this on tinycore linux if that helps at all. .

  • 1
    Wouldn't it be easier to launch the lp_server correctly from the start? – Kusalananda Jul 25 '18 at 10:37

You could use advanced regex (positive look behind/ahead) :

SAMPLESERIAL1=$(ps -o args | grep -Po '(?<=lp_server -n )[[:digit:]]+(?= -d \/dev\/usb\/lp1)')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy