Oracle pmon output needed

$ ps -ef | grep pmon | grep -v grep | awk '{ print $8 }' | cut -d '_' -f3
abc1
abc2
abc3
abc4
abc5
+ASM1

from above output i am looking exclude +ASM output and restout put is fine , also when i select any output and if is invalid from above output, shell script should exit

please me out in providing the shell script

--- Post updated at 11:33 AM ---

Hi
anyone please update on it

someone please provide your inputs

Rather that ps -ef | grep pmon | grep -v grep | awk ...... might I suggest ps -ef | grep [p]mon | ....... instead. The square brackets are a pattern match that has only one option, but because there is no grep process with this string in the process list, you can save a process and make your code clearer in one go.

If you are able to exclude +ASM , why not ignore that with a grep too?

You can probably get rid of the awk statement too by using different flags for ps, for example ps -eo args= | grep [p]mon | grep -v ASM might get you started.

I hope that this helps,
Robin

1 Like

Hi,

i am getting output as below,

$ ps -eo args= | grep [p]mon | grep -v ASM
ora_pmon_abc1
ora_pmon_abc2
ora_pmon_abc3
ora_pmon_abc4
ora_pmon_abc5

but i am looking output as below

abc1
abc2
abc3
abc4
abc5

Try

$ ps -eo args= |  awk -F"_" '/pmon/ && !/ASM/ {print $3}'
abc1
abc2
abc3
abc4
abc5
1 Like

Thanks Rudic

echo "======================================================"
echo "Enter the correct DB instance "
echo "======================================================"
ps -eo args= |  awk -F"_" '/pmon/ && !/ASM/ {print $3}'

################################
now from my script when i select any one of the instance from below output script should accept it, if i select wrong instance ..ie abc1111, then it should prompt like invalid entry and exit out

ps -eo args= |  awk -F"_" '/pmon/ && !/ASM/ {print $3}'
abc1
abc2
abc3
abc4
abc5

How about just not offering invalid entries? Did you consider bash 's (assuming this is your shell) select statement? Like

PS3="Enter the correct DB instance: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')
  do echo $CHOICE, $REPLY
  done
1) abc1
2) abc2
3) abc3
4) abc4
5) abc5
Enter the correct DB instance: 4
abc4, 4

Press <CTRL>D to exit the loop. Or, provide an extra item to quit.

below is my code, in below if i give invalid entry then it echo as "invalid entry" and should exit the script

#!/bin/bash
#set -x
PS3="Enter the correct DB instance: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')
  do echo $CHOICE
break
done

man bash :

Try

  [ ! $CHOICE ] && { echo "invalid entry"; break; }; 
1 Like

is this correct one i have udpated ?

#set -x
PS3="Enter the correct DB instance: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')[ ! $CHOICE ] && { echo "invalid entry"; break; }
  do echo $CHOICE
break
done

--- Post updated at 01:20 PM ---

Thanks

its working now

#!/bin/bash
#set -x
PS3="Enter the correct DB instance: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')
 do echo $CHOICE
[ ! $CHOICE ] && { echo "You have selected invalid Database"; break; };
break
done

--- Post updated at 01:21 PM ---

Thanks

its working now

#!/bin/bash
#set -x
PS3="Enter the correct DB instance: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')
do echo $CHOICE
[ ! $CHOICE ] && { echo "You have selected invalid Database"; break; };
break
done

The "compound command" including the break is not needed if you break out of the loop anyhow:

do echo $CHOICE
 [ ! $CHOICE ] && echo "You have selected invalid Database"
 break

But you can also run some code within the select loop, and "return" to the menu until finished with all databases.

1 Like
#set -x
PS3="Enter the correct DB instance number: "
select CHOICE in $(ps -eo args= | awk -F"_" '/pmon/ && !/ASM/ {print $3}')
 do echo $CHOICE
[ ! $CHOICE ] && { echo "You have selected invalid Database from above list"; break; };
break
done

Hi rudic
i will be selecting only 1 DB at a time, now 2nd condition is, if i select correct Db instance it should connect the DB else exit

please help

Now, wouldn't that be something that you can do on your own; no - you must do on your own? No one in here knows what DB you run and how to connect to it. And even if someone knew - do you really want to be spoonfed with readymade turnkey solutions?