count multiple objects in shell script

Hi all:

Trying to count the number of oracle instances on HPUX 11.23 - using ksh. I have multiple instances running and I would like to have a count for how many processes for each instance. Example, run the 'ps -efu oracle' command and for each instance increment a counter. So I am looking for
INT1=# INT2=# INT3=# ... INTn=#

I keep thinking a 'do-while' or some type of 'while' loop but I am missing something, somewhere...

Many thanks!

As a first attempt:

ps -ef | grep -v awk | awk -F_ '/ora_.*/ {I[$NF]++} END { for( i in I ) print i "=" I }'

I only have one DB locally, it gives:

DB1=24

But tried it on a productive (albeit AIX) Oracle server and gives the right result too.

1 Like

Kinda, sorta. The '_' delimiter is only picking up the ckpt, lgwr, pmon, smon, etc processes. Not getting the connections (LOCAL=). The format of the output is crucial as well since I am wanting to append performance data for nagios. I need the output to look like;
$INT1=# $INT2=# $INT3=# ... $INTn=# | $INT1 $INT2 $INT3 ... $INTn

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
You may need to adjust the ps syntax for your Unix platform.

ps -eu oracle -o args |
  awk 'END {
    for (I in inst)
      print I, "=>", inst
    }
  /^ora(_|cle)/ {
    sub(/^oracle/, null)
    sub(/^.*_/, null)
    inst[$1]++
    }'

Actually, given the requested format:

ps -eu oracle -o args |
  awk 'END {
    for (I in inst)
	  printf "%s = %d ", I, inst
    printf "%s ", "|"	  
	for (I in inst)
      printf "%s ", I
    print	  
	}
  /^ora(_|cle)/ {
    sub(/^oracle/, null)
	sub(/^.*_/, null)
	inst[$1]++
	}'

---------- Post updated at 09:27 PM ---------- Previous update was at 09:15 PM ----------

On HP-UX you'll need something like this:

UNIX95= ps -eu oracle -o args |
  nawk 'END {
    for (I in inst)
      printf "%s = %d ", I, inst
    printf "%s ", "|"      
    for (I in inst)
      printf "%s ", I
    print      
    }
  /^ora(_|cle)/ {
    sub(/^oracle/, null)
    sub(/^.*_/, null)
    inst[$1]++
    }'
2 Likes

Many thanks to both of you. radoulov did the trick.