PDA

View Full Version : Help required on "grep"


grbabu1
03-25-03, 18:19
I am new to shell scripting. My question is

How to grep exact pattern from a variable in UNIX.

This is the scenario.

In the present server there are 3 oracle databases up and running, which are HRM83DEV, HRM83TST, HRM83CFG. There is no database/s called HRM83LOG, HRM.

This is what I wrote in the shell script

=======================

# script name shtest

if [ X$1 != X ]; then
DBNAME=$1
export DBNAME
else
echo "Database name must be specified as a parameter"
exit
fi

ECH=`ps -fu oracle | grep $DBNAME |grep -c ora_`
if [ "$ECH" = "0" ]; then
echo "The database is not UP and RUNNING, not able to perform backup"
exit
fi

======================

When I Run the script

1. shtest HRM83DEV
(no output, whcih is fine because I did not give any dispaly message)

2. shtest HRM83LOG
The database is not UP and RUNNING, not able to perform backup

3. shtest HRM
(no output like 1st run)

in the 3rd run I am getting nothing. I would like to get as like 2nd run
"The database is not UP and RUNNING, not able to perform backup".

I understand
ps -fu oracle | grep $DBNAME |grep -c ora_ will search the patter starts with H and on.....

I appreciate if anybody help me on this to grep exact pattern search.

Thank you,

sathyaram_s
03-27-03, 12:36
Can you tell what is meant by "exact pattern search" ...

Sorry ...

Cheers

Sathyaram


Originally posted by grbabu1
I am new to shell scripting. My question is

How to grep exact pattern from a variable in UNIX.

This is the scenario.

In the present server there are 3 oracle databases up and running, which are HRM83DEV, HRM83TST, HRM83CFG. There is no database/s called HRM83LOG, HRM.

This is what I wrote in the shell script

=======================

# script name shtest

if [ X$1 != X ]; then
DBNAME=$1
export DBNAME
else
echo "Database name must be specified as a parameter"
exit
fi

ECH=`ps -fu oracle | grep $DBNAME |grep -c ora_`
if [ "$ECH" = "0" ]; then
echo "The database is not UP and RUNNING, not able to perform backup"
exit
fi

======================

When I Run the script

1. shtest HRM83DEV
(no output, whcih is fine because I did not give any dispaly message)

2. shtest HRM83LOG
The database is not UP and RUNNING, not able to perform backup

3. shtest HRM
(no output like 1st run)

in the 3rd run I am getting nothing. I would like to get as like 2nd run
"The database is not UP and RUNNING, not able to perform backup".

I understand
ps -fu oracle | grep $DBNAME |grep -c ora_ will search the patter starts with H and on.....

I appreciate if anybody help me on this to grep exact pattern search.

Thank you,

grbabu1
03-28-03, 13:06
Sathyaram,

Example:

If I give

ps -fu oracle |grep HRM

Here I mean HRM is the exact pattern search

it is giving process in oracle all start with HRM, also HRM83, HRM83TST and so on...but what I need is only HRM not HRM83 or HRM83TST....

Thank you,

sathyaram_s
03-28-03, 14:19
grep is designed so ...

Assuming HRM to be a word in the output, you can use

ps -fu oracle | grep " HRM "

ie, a blank on either side of HRM ...

or, if you will have HRM as the first word in a sentence also , then

egrep "HRM | HRM "

Hope this helps

Cheers

Sathyaram

Originally posted by grbabu1
Sathyaram,

Example:

If I give

ps -fu oracle |grep HRM

Here I mean HRM is the exact pattern search

it is giving process in oracle all start with HRM, also HRM83, HRM83TST and so on...but what I need is only HRM not HRM83 or HRM83TST....

Thank you,