Get Variables from Grep Output
Clash 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.
.
linux bash scripting grep busybox
migrated from serverfault.com Jul 25 at 10:34
This question came from our site for system and network administrators.
add a comment |Â
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.
.
linux bash scripting grep busybox
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 thelp_server
correctly from the start?
â Kusalananda
Jul 25 at 10:37
add a comment |Â
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.
.
linux bash scripting grep busybox
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.
.
linux bash scripting grep busybox
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 thelp_server
correctly from the start?
â Kusalananda
Jul 25 at 10:37
add a comment |Â
1
Wouldn't it be easier to launch thelp_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
add a comment |Â
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)')
add a comment |Â
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)')
add a comment |Â
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)')
add a comment |Â
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)')
You could use advanced regex (positive look behind/ahead) :
SAMPLESERIAL1=$(ps -o args | grep -Po '(?<=lp_server -n )[[:digit:]]+(?= -d /dev/usb/lp1)')
answered Jul 25 at 12:03
kaliko
1058
1058
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Wouldn't it be easier to launch the
lp_server
correctly from the start?â Kusalananda
Jul 25 at 10:37