I have ran into a small issue and I am not sure how to fix it.
In one of our current scripts we have this line which does a grep to get the pid of the process.
ps -ef | grep nco_p_syslog | grep $x | awk '{print $2}'
However this is not returning anything due to the how long the value of $x is. Which a sample value is...
ot1p_stdby
However as you can see a simple grep cuts this off...
tivoli 20343 1 0 10:16:09 pts/1 0:00 /lcl/apps/Tivoli/netcool/omnibus/probes/solaris2/nco_p_syslog -manager ot1p_std
I need to find a way to do a grep command like below only have it know off the _stdby contained within $x.
So that even though $x contains the value ot1p_stdby it should cutt off the _stdby and only by ot1p when it does the following command.,,,
ps -ef | grep nco_p_syslog | grep $x | awk '{print $2}'
I was not sure if there is a way to do this right in the command above.
Thanks
Ad hoc (untested):
ps -ef | grep nco_p_syslog | grep "${x:0:8}" | awk '{print $2}'
Gave it a try but got this...
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
./test.sh[3]: "${x:0:8}": bad substitution
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>
Here is how I am testing...
#!/bin/ksh
x=entp_stdby
ps -ef | grep nco_p_syslog | grep "${x:0:8}" | awk '{print $2}'
Changed the " to ' and that fixed the error...
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
./test.sh[3]: '${x:0:8}': bad substitution
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>
But still no results. Seems like that would have worked. I see where you was going with it. It should have take the $x which has a value of entp_stdby and only did a grep for entp_std but it returned no results.
As you can see it should have returned results as this is running out there.
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>ps -ef | grep syslog | grep entp_std
tivoli 20337 1 0 10:16:09 pts/1 0:01 /lcl/apps/Tivoli/netcool/omnibus/probes/solaris2/nco_p_syslog -manager entp_std
Tytalus
October 23, 2009, 11:26am
4
it's likely not the grep; it's probably the initial ps.
Assuming you're on solaris:
/usr/ucb/ps -wwaxu | grep "nco_p_syslog .*$x" | awk '{print $2}'
should work...
Yep now that works. It also shows the pid for the grep itself though as well. I am trying to figure out how this is working, lol. Lost me on this one.
How can I make it so it does not return 2 pids and only the one I am looking for.
Right now its returning the pid for this grep as well.
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
10572
20337
tytalus:
it's likely not the grep; it's probably the initial ps.
Assuming you're on solaris:
/usr/ucb/ps -wwaxu | grep "nco_p_syslog .*$x" | awk '{print $2}'
should work...
try this:
/usr/ucb/ps -wwaxu | grep "[n]co_p_syslog .*$x" | awk '{print $2}'
bash and i beilve ksh (the later one) support this type of substitution:
~> var1=nco_p_syslog
~> echo $var1
nco_p_syslog
~> echo ${var1/_syslog/}
nco_p
~> echo ${var1/_syslog/_newword}
nco_p_newword
~>
That's interesting, as on my workstation (Debian Linux 5 feat. Bash), this works ...
[house@leonov] a='nco_p_syslog -manager ot1p_std'; x='ot1p_stdby'; echo $a | grep "${x:0:8}"
nco_p_syslog -manager ot1p_std
Anyway, how about skipping single as well as double quotes entirely, as follows ...
[house@leonov] a='nco_p_syslog -manager ot1p_std'; x='ot1p_stdby'; echo $a | grep ${x:0:8}
nco_p_syslog -manager ot1p_std