LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   capture the output to a variable (https://www.linuxquestions.org/questions/linux-general-1/capture-the-output-to-a-variable-481330/)

Melsync 09-07-2006 01:23 PM

capture the output to a variable
 
I want to assign the value of a certain pid in order to kill it in a script
Quote:

echo `ps ux | grep dhcpcd | grep -v grep` | cut -d' ' -f2`
gets me the pid number, and only that, all right but then
Quote:

pidno=`echo `ps ux | grep dhcpcd | grep -v grep` | cut -d' ' -f2``
returns 'command not found' and
Quote:

pidno="echo `ps ux | grep dhcpcd | grep -v grep` | cut -d' ' -f2`"
returns the whole string of the output of the command ps.
How can I put the single number in a variable?
Thanks.

acid_kewpie 09-07-2006 01:27 PM

i'm guessing you've not heard aboput the pidof command?
Code:

pidno=$(pidof dhcpcd)
as far as where you're going wrong... take out all the echo stuff. you need to echo it to show the output as a command but you don't need it to assign the output to a vairable.
Code:

pidno=$(ps ux | ... )

homey 09-07-2006 01:51 PM

Other than typo in dhcpd ( dhcpcd ) I would go with
Code:

pidno=$(pidof dhcpd)
echo $pidno
1995

With correct spelling this works also
Code:

pidno=$(echo `ps ux | grep dhcpd | grep -v grep` | cut -d' ' -f2)
echo $pidno
1995


Melsync 09-07-2006 02:15 PM

Thank you, all four solutions work.
Btw, homey, the process is not a typo; I'm using this program: dhcpcd at freshmeat
Thanks again. :)

acid_kewpie 09-07-2006 02:17 PM

Quote:

Originally Posted by Melsync
Thank you, all four solutions work.
Btw, homey, the process is not a typo; I'm using this program: dhcpcd at freshmeat
Thanks again. :)

hehe, bless. dhcpd = dhcp server daemon. dhcpcd = dhcp client daemon.


All times are GMT -5. The time now is 10:11 AM.