How can I store the Tnsping output in a variable?

1.6k Views Asked by At

launching from shell thee command :

tnsping myDB

i obtain the output :

OK (1 msec)

How can i store this output in a variable so i can test if is OK?

1

There are 1 best solutions below

0
On BEST ANSWER

You can store the output of a command using command substitution.

However, you said you wanted to check the result. If you store that string output you would need to parse it to only look for 'OK' (since the ping time might differ), and deal with the banner info etc. (though that is relatively easy).

It would be simpler to look at the return code from from the tnsping command rather than its output:

tnsping myDB
echo $?

You can test the value of $?. If the ping was OK then it will be zero; otherwise it will be 1.

You haven't said what you want to do with the test result. If you want to show the error (if there is one) and nothing if it worked, you could combine both:

RESULT=$( tnsping myDB )
if [[ $? -ne 0 ]]; then
  printf "Ping failed, output was\n\n"
  printf "%s\n" "${RESULT}"
fi