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
You could always grep it, and test $?
$? 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?
foo=$(grep -c bar /baz)
Yep, that works.
Thanks, my korn script skills are a little rusty.
Yeah, it's POSIX defined. You can also use backticks ( ` ` ).