cancel
Showing results for 
Search instead for 
Did you mean: 

loop on ps -ef output

SOLVED
Go to solution
Leo The Cat
Regular Advisor

loop on ps -ef output

Hi Guys


I'm doing this

for databases in $(ps -ef | grep -i ora_pmon | awk '{print $9}')
> do
> echo $databases
> done
Result is:

ora_pmon_WSLPREP1
ora_pmon_HCMPRE21
ora_pmon_WMTPREP1
ora_pmon_RATPREP1
-i
ora_pmon_PSCPREP1
ora_pmon_AUSTRNG1
ora_pmon_AUSPREP1

1. How to Eliminate the -i (in the loop because grep command is running)

2. How to eliminate "ora_pmon_" and "1" to have only my instance name like this:

WSLPREP
HCMPRE2
WMTPREP
RATPREP
PSCPREP
AUSTRNG
AUSPREP


Let me know if you have a solution.

Thanks in advance
Regards
Den
9 REPLIES
Kevin Wright
Honored Contributor

Re: loop on ps -ef output

grep -i [o]ra_pmon

echo $databases |awk -F'_' '{print $3}'
Wilfred Chau_1
Respected Contributor
Solution

Re: loop on ps -ef output

$(ps -ef | grep -v grep | awk '{print substr($9,10,length($9) - 10)}')

note:
grep -v grep : to not grab the greb command

substr of string $9, starting at position 10, to position 11 to second last character.

Wilfred Chau_1
Respected Contributor

Re: loop on ps -ef output

I think I missed the ora_pmon part, here you go again.

$(ps -ef |grep -i ora_pmon | grep -v grep | awk '{print substr($9,10,length($9) - 10)}')

macosta
Trusted Contributor

Re: loop on ps -ef output

As pointed out above, you can avoid finding grep in the ps listing by messing with shell expansion a bit and adding [] around the first letter.

There's no real reason to call grep multiple times, nor even to call awk over and over again just for simple string manipulation. Here's one way to do it:
--
#! /bin/bash

for database in $(ps -ef |grep [o]ra_pmon); do
printf '%s\n' "${database##*_}"
done
--

This way, you only run 'ps' once and 'grep' once, and printf is a shell builtin in bash (even if there is a binary printf in the filesystem, bash won't use it unless you specify the full pathname.)You could probably do the whole thing with shell builtins and the /proc filesystem, but the benefit would be negligible in most cases.
macosta
Trusted Contributor

Re: loop on ps -ef output

Hm, the above was untested, so I'm not sure the loop will work like I initially thought.

You might try this:
--
#! /bin/bash

ps -ef |grep [o]ra_pmon | while read db; do
printf '%s\n' "${db##*_}"
done
--
Huc_1
Honored Contributor

Re: loop on ps -ef output

Hi Den

This works for me notice {print $8} you may need to adapt this to your output.

for databases in $(ps -ef | grep -i [o]ra_pmon |grep -v "grep" | awk '{print $8}')
> do
> echo $databases
> done

enjoy life.

Jean-Pierre Huc
Smile I will feel the difference
Huc_1
Honored Contributor

Re: loop on ps -ef output

Sorry got distracted and send the wrong version

--------------------------
This works for me notice {print $8} you may need to adapt this to your output.

for databases in $(ps -ef | grep -i [o]ra_pmon |grep -v "grep" | awk '{print substr($8,10)}')
> do
> echo $databases
> done

--------------------------
if you need to replace $8 by $9

Hoping this is a little clear.

enjoy life.

Jean-Pierre Huc
Smile I will feel the difference
Leo The Cat
Regular Advisor

Re: loop on ps -ef output

Excellent !
Thanks Guys
Steven Schweda
Honored Contributor

Re: loop on ps -ef output

> [...] grep -i [o]ra_pmon |grep -v "grep"
> [...]

So, you completely missed the reason for
using that "[o]" construct in "[o]ra_pmon"?

> This works for me [...]

You should try it without the second grep
command.