Unix Shell - comparing PIDs in shell

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > comparing PIDs in shell





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author comparing PIDs in shell
marconi

2007-12-19, 1:23 pm

Hi,

There is a file having a list of running PIDs and another file having
a list of registered PIDs. How can we check if the number of running
PIDs are less or more than the registered PIDs, comparing the total
no. in each and also the values.

Request you to pls give your inputs.

Thanks a lot in advance.

Joachim Schmitz

2007-12-19, 1:23 pm

"marconi" <kirankumar.techy@gmail.com> schrieb im Newsbeitrag
news:d291c279-635c-4992-8670-ed8997ac0d58@y5g2000hsf.googlegroups.com...
> Hi,
>
> There is a file having a list of running PIDs and another file having
> a list of registered PIDs. How can we check if the number of running
> PIDs are less or more than the registered PIDs, comparing the total
> no. in each and also the values.
>
> Request you to pls give your inputs.


if [ ${a:=$(wc -l file1)} -lt ${b:=$(wc -l file2)} ]
then
echo less lines in file1 than in file2
elif [ $a -gt $b ]
then
echo more lines in file1 than in file2
else
echo same number of lines in both files
fi

Bye, Jojo


Ed Morton

2007-12-19, 1:23 pm



On 12/19/2007 9:11 AM, marconi wrote:
> Hi,
>
> There is a file having a list of running PIDs and another file having
> a list of registered PIDs. How can we check if the number of running
> PIDs are less or more than the registered PIDs, comparing the total
> no. in each and also the values.
>
> Request you to pls give your inputs.
>
> Thanks a lot in advance.
>


You should be able to tweak this to your needs, untested (check the case/spelling!):

awk 'NR==FNR{ reg[$0]; nReg++; next }
$0 in reg { regRun[$0]; nRegRun++; next }
{ unregRun[$0]; nUnregRun++ }
END { for (pid in reg) {
if ( (pid in regRun) || (pid in unregRun) ) {
unrunReg[pid]
nUnrunReg++
}
printf "Registered (%d):\n", nReg
for (pid in reg) print pid
printf "Registered and running (%d):\n", nRegRun
for (pid in regRun) print pid
printf "Registered but not running (%d):\n, nUnrunReg
for (pid in unregRun) print pid
printf "Running but not registered (%d):\n", nUnregRun
for (pid in unregRun) print pid
}' registered running

Regards,

Ed.

Janis

2007-12-19, 1:23 pm

On 19 Dez., 16:11, marconi <kirankumar.te...@gmail.com> wrote:
> Hi,
>
> There is a file having a list of running PIDs and another file having
> a list of registered PIDs. How can we check if the number of running
> PIDs are less or more than the registered PIDs, comparing the total
> no. in each and also the values.


awk '
NR==FNR { regPID[$1]; reg++; next }
{ run++ }
$1 in regPID { reg_and_run++ }
END { print run, reg, reg_and_run
if (reg < run) print "less registered"
if (reg > run) print "less running"
# etc.
}
' file_with_registeredPIDs file_with_runningPIDs

Janis

>
> Request you to pls give your inputs.
>
> Thanks a lot in advance.

Janis

2007-12-19, 1:23 pm

On 19 Dez., 16:20, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
> "marconi" <kirankumar.te...@gmail.com> schrieb im Newsbeitragnews:d291c279-635c-4992-8670-ed8997ac0d58@y5g2000hsf.googlegroups.com...
>
>
>
>
> if [ ${a:=$(wc -l file1)} -lt ${b:=$(wc -l file2)} ]


In what shell does that work?

Mind that wc -l file produces other output than wc -l <file

> then
> echo less lines in file1 than in file2
> elif [ $a -gt $b ]
> then
> echo more lines in file1 than in file2
> else
> echo same number of lines in both files
> fi


Your program does not consider the actual PID values in the sets,
rather only the number of lines (resp. number of PIDs).

Janis

> Bye, Jojo


Joachim Schmitz

2007-12-19, 1:23 pm

"Janis" <janis_papanagnou@hotmail.com> schrieb im Newsbeitrag
news:6a66f512-2862-4c02-8866-77e97fd8071f@s12g2000prg.googlegroups.com...
> On 19 Dez., 16:20, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> wrote:
>
> In what shell does that work?

ksh, bash

> Mind that wc -l file produces other output than wc -l <file

ops, true, so let's use wc -l <file then

>
> Your program does not consider the actual PID values in the sets,
> rather only the number of lines (resp. number of PIDs).

That was his 1st objective. I missed the 2nd.

Bye, Jojo


Janis

2007-12-19, 1:23 pm

On 19 Dez., 16:46, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
> "Janis" <janis_papanag...@hotmail.com> schrieb im Newsbeitragnews:6a66f512-2862-4c02-8866-77e97fd8071f@s12g2000prg.googlegroups.com...
>
>
>
>
>
>
>
> ksh, bash


Not in the ksh I am currently using, neither in my bash.

>
> ops, true, so let's use wc -l <file then


But it doesn't seem to fix your construct either.
At least on WinDOS (MKS ksh, Cygwin bash) it produces errors.

Janis
Joachim Schmitz

2007-12-19, 1:23 pm

"Janis" <janis_papanagnou@hotmail.com> schrieb im Newsbeitrag
news:9d1502ef-2c20-4a4f-adcf-5af87c01e1db@r60g2000hsc.googlegroups.com...
> On 19 Dez., 16:46, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> wrote:
>
> Not in the ksh I am currently using, neither in my bash.
>
>
> But it doesn't seem to fix your construct either.
> At least on WinDOS (MKS ksh, Cygwin bash) it produces errors.

Works fine here, with the wc -l <file.
might fail is a or b are set so do an unset a b first

Bye, Jojo


Janis Papanagnou

2007-12-19, 1:23 pm

Joachim Schmitz wrote:
> "Janis" <janis_papanagnou@hotmail.com> schrieb im Newsbeitrag
> news:9d1502ef-2c20-4a4f-adcf-5af87c01e1db@r60g2000hsc.googlegroups.com...
>
>
> Works fine here, with the wc -l <file.
> might fail is a or b are set so do an unset a b first


No, that's not the reason. Because the construct you proposed is unsafe
in that respect I already did the variable initialization anyway.

Curiously I had played a bit around with that code and it seemed to me
it might be the effect of some spurious '\r'. Other less conservative
constructs like [[...]] and ((...)) don't seem to show that problem.
But I haven't spent too much time to try to fix or examine that further.

Janis

>
> Bye, Jojo
>
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com