need help with grep in eval
need help with grep in eval
am 22.12.2004 14:43:17 von sp00fd
I have the following script which I would like to be able to use as
such:
a) psscript "inetd -[st] -[st]"
b) psscript "someproc | grep -v something"
c) psscript utmpd
I seem to be able to accomplish a or b, but not both at the same time.
Code follows (currently works for case a and c, not b)
#!/usr/bin/ksh
proc="$@"
pscmd=""
ostype=$(uname -s)
SOLARIS_PSCMD="ps -ef"
solaris_ps() {
proc=$1
# The quoting around "$proc" below is seemingly where the
# problem lies. With quotes I can accomplish case a, without
# quotes I can accomplish case b
eval "$SOLARIS_PSCMD | grep -v $0 | grep -v grep | grep \"$proc\""
}
if [[ -z $proc ]]; then
print -u2 "No argument given" && exit 2
fi
if [[ $ostype = SunOS ]]; then
pscmd=solaris_ps
fi
$pscmd "$proc" | wc -l
Re: need help with grep in eval
am 22.12.2004 17:05:18 von cfajohnson
On Wed, 22 Dec 2004 at 13:43 GMT, sp00fd@yahoo.com wrote:
> I have the following script which I would like to be able to use as
> such:
>
> a) psscript "inetd -[st] -[st]"
> b) psscript "someproc | grep -v something"
What are you trying to do? Do you want:
psscript "$(someproc | grep -v something)"
> c) psscript utmpd
>
> I seem to be able to accomplish a or b, but not both at the same time.
> Code follows (currently works for case a and c, not b)
What doesn't work? What errors do you get?
>
> #!/usr/bin/ksh
>
> proc="$@"
> pscmd=""
> ostype=$(uname -s)
> SOLARIS_PSCMD="ps -ef"
>
> solaris_ps() {
> proc=$1
> # The quoting around "$proc" below is seemingly where the
> # problem lies. With quotes I can accomplish case a, without
> # quotes I can accomplish case b
> eval "$SOLARIS_PSCMD | grep -v $0 | grep -v grep | grep \"$proc\""
Why are you using eval?
> }
>
> if [[ -z $proc ]]; then
> print -u2 "No argument given" && exit 2
> fi
>
> if [[ $ostype = SunOS ]]; then
> pscmd=solaris_ps
> fi
>
> $pscmd "$proc" | wc -l
If you are not running this on SunOS, pscmd is empty; is that what
you want?
>
>
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
============================================================ =======
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Re: need help with grep in eval
am 22.12.2004 18:22:00 von sp00fd
Ok, I guess I'll have to explain the background a bit. Basically, this
is part of a Tivoli process monitor that I'm building. The monitor
itself is written in javascript (not by choice), so I shell out to this
program to "ps | grep" for the process, returning the number of
processes found. The processes are specified in a configuration file,
and I would like this to be as flexible as possible. The config file
format looks like:
# proc name:proc regex:minimum required:action:severity:poc:extra text
inetd:inetd -[st] -[st]:1:/etc/init.d/inetsvc start:WARNING:Unix Team:.
someproc:someproc | grep -v
something:4:/usr/local/bin/start_procs:CRITICAL:Unix Team:.
So, within my javascript, the second field here (proc regex) will be
passed to the shell script that I pasted earlier. So, the pseudo code
looks a bit like:
for each line in the config file
run the psscript with the second field as the argument
num_running equals the output of psscript
if num_running < min_required
if we've tried to start it once already, post a message that
we're having a problem.
else run the action command if it exists to try to start the
process
So, the point is that I'd like this to be very flexible. I'd like to
be able to pass a simple process name to the script, or a regular
expression, or even a pipe.
The eval exists because it's the only way that I can get it to use the
pipe (as seen in the config file example).
Re: need help with grep in eval
am 22.12.2004 18:30:53 von sp00fd
Sorry, just realized I failed to answer a couple of your questions.
Basically as seen, the script's output is wrong:
$ psscript "inetd -[st] -[st]"
1
$ psscript "inetd"
1
$ psscript "inetd | grep -v foo"
0 # <- should be 1 and is 1 if I change \"$proc\" to $proc in the
solaris_ps(), although that causes the first case to fail
Lastly, if I'm not running SunOS, yes, pscmd is empty. I'm ok with
that for now. I just wanted to write it so that it's easy to extend if
need be, but everything this will run on will be SunOS.
Re: need help with grep in eval
am 22.12.2004 18:57:36 von cfajohnson
On Wed, 22 Dec 2004 at 17:30 GMT, sp00fd@yahoo.com wrote:
> Sorry, just realized I failed to answer a couple of your questions.
Please quote the relevant parts of the post to which you are replying.
> Basically as seen, the script's output is wrong:
>
> $ psscript "inetd -[st] -[st]"
> 1
> $ psscript "inetd"
> 1
> $ psscript "inetd | grep -v foo"
> 0 # <- should be 1 and is 1 if I change \"$proc\" to $proc in the
> solaris_ps(), although that causes the first case to fail
I don't remember what you had there.
> Lastly, if I'm not running SunOS, yes, pscmd is empty. I'm ok with
> that for now.
What do you think will happen if you execute that command with an
empty $pscmd? Use "pscmd=:" instead.
> I just wanted to write it so that it's easy to extend if
> need be, but everything this will run on will be SunOS.
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
============================================================ =======
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Re: need help with grep in eval
am 22.12.2004 19:36:34 von sp00fd
I'm using google groups, which apparently doesn't do quoting anymore...
Well, let's just get to the nitty gritty. How can I get the following
to act right in each situation:
proc="inetd -[st] -[st]"
ps -ef | grep "$proc"
proc="inetd | grep -v foobar"
ps -ef | grep "$proc"
Re: need help with grep in eval
am 22.12.2004 20:25:48 von cfajohnson
On Wed, 22 Dec 2004 at 18:36 GMT, sp00fd@yahoo.com wrote:
> I'm using google groups, which apparently doesn't do quoting anymore...
Then you'd better use something that works if you expect to get
help in the newsgroups. If nothing else, can't you cut and paste?
> Well, let's just get to the nitty gritty. How can I get the following
> to act right in each situation:
What is "right"?
What isn't happening the way you want?
What do you want to happen?
> proc="inetd -[st] -[st]"
> ps -ef | grep "$proc"
> proc="inetd | grep -v foobar"
> ps -ef | grep "$proc"
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
============================================================ =======
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Re: Need help with grep
am 29.03.2008 11:21:19 von Maxwell Lol
explor writes:
> Hi Gurus,
>
[snip]
I think you have line wrapping.
How many lines are the following?
> 07.JavaMail.remedy@happy.test.com>, proto=ESMTP, daemon=MTA,
> relay=happy.test.com [12.13.2.24]
> Mar 16 05:48:30 mta-rwc-1.test.com sm-mta[15538]: [ID 801593
> mail.info] m2GCm3bI015538: from=, size=760,
> class=0, nrcpts=1,
> msgid=<14341520.1205671710508.JavaMail.remedy@happy.test.com>,
> proto=ESMTP, daemon=MTA, relay=happy.test.com [128.137.26.24] Mar 16
> 05:48:30 mta-rwc-1.test.com sm-mta[15538]: [ID 801593 mail.info]
> m2GCm3bJ015538: from=, size=755, class=0,
> nrcpts=1,
> msgid=<18840257.1205671710729.JavaMail.remedy@happy.test.com>,
> proto=ESMTP, daemon=MTA, relay=happy.test.com [12.13.26.24]
It looks like a sendmail log. If so, the first line should start with
a timestamp, and it starts with "07.JavaMail.remedy@happy.test.com>"
That makes it very hard to test to see if things works properly
because you did not give us a correct input sample.
I think the sample input is
Mar 16 05:48:30 mta-rwc-1.test.com sm-mta[15538]: [ID 801593 mail.info] m2GCm3bI015538: from=, size=760, class=0, nrcpts=1, msgid=<14341520.1205671710508.JavaMail.remedy@happy.test.com>, proto=ESMTP, daemon=MTA, relay=happy.test.com [128.137.26.24] Mar 16
05:48:30 mta-rwc-1.test.com sm-mta[15538]: [ID 801593 mail.info] m2GCm3bJ015538: from=, size=755, class=0, nrcpts=1, msgid=<18840257.1205671710729.JavaMail.remedy@happy.test.com>, proto=ESMTP, daemon=MTA, relay=happy.test.com [12.13.26.24]
Re: Need help with grep
am 29.03.2008 11:25:02 von Maxwell Lol
explor writes:
> #/bin/ksh
> SIZE1=20971520
> for i in `grep "size=" $LOG | awk '{print $11}' | awk -F= '{print $2}'
> | sed 's/\,//'`
> do
The sample input you provided does not contain any examples of the
string "size=" so it very hard to debug your script.
Re: Need help with grep
am 31.03.2008 20:07:54 von bhaveshah
On Mar 29, 3:25=A0am, Maxwell Lol wrote:
> explor writes:
> > #/bin/ksh
> > SIZE1=3D20971520
> > for i in `grep "size=3D" $LOG | awk '{print $11}' | awk -F=3D '{print $2=
}'
> > | sed 's/\,//'`
> > do
>
> The sample input you provided does not contain any examples of the
> string "size=3D" so it very hard to debug your script.
Sorry, Here is the correct log output:
Mar 16 03:44:34 mta-rwc-1.test.com sm-mta[23527]: [ID 801593
mail.info] m2GAiBIa023527: from=3D, size=3D754,
class=3D0, nrcpts=3D1,
msgid=3D<22271029.1205664274109.JavaMail.remedy@happy.test.com>,
proto=3DESMTP, daemon=3DMTA, relay=3Dhappy.test.com [12.13.26.24]
Re: Need help with grep
am 01.04.2008 21:08:26 von dave+news002
explor wrote:
> On Mar 29, 3:25 am, Maxwell Lol wrote:
>> explor writes:
>> > #/bin/ksh
>> > SIZE1=20971520
>> > for i in `grep "size=" $LOG | awk '{print $11}' | awk -F= '{print $2}'
>> > | sed 's/\,//'`
>> > do
>>
>> The sample input you provided does not contain any examples of the
>> string "size=" so it very hard to debug your script.
>
> Sorry, Here is the correct log output:
>
> Mar 16 03:44:34 mta-rwc-1.test.com sm-mta[23527]: [ID 801593
> mail.info] m2GAiBIa023527: from=, size=754,
> class=0, nrcpts=1,
> msgid=<22271029.1205664274109.JavaMail.remedy@happy.test.com>,
> proto=ESMTP, daemon=MTA, relay=happy.test.com [12.13.26.24]
SIZE1=20971520
awk -F 'size=' -v t="$SIZE1" '
($2 + 0) > t && match($0, /msgid=[^>]+>/) {
print substr($0, RSTART + 6, RLENGTH - 6)
}' "$LOG"