Get Variables from Grep Output

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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
else:
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.
.







share|improve this question













migrated from serverfault.com Jul 25 at 10:34


This question came from our site for system and network administrators.










  • 1




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














up vote
0
down vote

favorite












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
else:
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.
.







share|improve this question













migrated from serverfault.com Jul 25 at 10:34


This question came from our site for system and network administrators.










  • 1




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












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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
else:
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.
.







share|improve this question













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
else:
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.
.









share|improve this question












share|improve this question




share|improve this question








edited Jul 25 at 10:47









ctrl-alt-delor

8,68331947




8,68331947









asked Jul 4 at 11:30







Dan











migrated from serverfault.com Jul 25 at 10:34


This question came from our site for system and network administrators.






migrated from serverfault.com Jul 25 at 10:34


This question came from our site for system and network administrators.









  • 1




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












  • 1




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







1




1




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




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










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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



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





share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458318%2fget-variables-from-grep-output%23new-answer', 'question_page');

    );

    Post as a guest





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













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



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





    share|improve this answer

























      up vote
      0
      down vote













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



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





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









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



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





        share|improve this answer













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



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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 25 at 12:03









        kaliko

        1058




        1058






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458318%2fget-variables-from-grep-output%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay