Results 1 to 6 of 6
I need to capture the return value from a 'grep' command in a korn script.
I know the value of the grep statement, if it finds the oracle SID it's ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-20-2005 #1Just Joined!
- Join Date
- Mar 2005
- Posts
- 33
return from grep command
I need to capture the return value from a 'grep' command in a korn script.
I know the value of the grep statement, if it finds the oracle SID it's 2. I need to redirect that return to a variable so that I can evaluate the return and decide whether to either startup the database or not.
This is what I have in my script so far:
value=ora_pmon_$sid -> $sid retrieved from file
ps -ef | grep $value > greptest.txt -> places values in file greptest.txt
return=grep -c ora_pmon greptest.txt -> here's my problem, I know that the answer is going to be either 1 or 2. How to I put the value in a variable I can test? Then I can test it using something like:
if (( return == 2 )) then
function x
etc...
- 10-20-2005 #2
You could always grep it, and test $?
- 10-20-2005 #3Just Joined!
- Join Date
- Mar 2005
- Posts
- 33
$? returns the status of the command, not the results of the '-c' argument.
Note the '-c' argument in the grep command. This will return the number of matches, which is what I'm looking for.
Rather than return the results of the -cto the screen, I what to return the results to a variable?
- 10-20-2005 #4Code:
foo=$(grep -c bar /baz)
- 10-20-2005 #5Just Joined!
- Join Date
- Mar 2005
- Posts
- 33
Yep, that works.
Thanks, my korn script skills are a little rusty.
- 10-20-2005 #6
Yeah, it's POSIX defined. You can also use backticks ( ` ` ).


Reply With Quote
