Find the answer to your Linux question:
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.
  1. #1
    Just 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...

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    You could always grep it, and test $?

  3. #3
    Just 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?

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    foo=$(grep -c bar /baz)

  6. #5
    Just Joined!
    Join Date
    Mar 2005
    Posts
    33
    Yep, that works.

    Thanks, my korn script skills are a little rusty.

  7. #6
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Yeah, it's POSIX defined. You can also use backticks ( ` ` ).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •