如何将 Tnspi​​ng 输出存储在变量中?

How can I store the Tnsping output in a variable?

从 shell 你的命令启动:

访问 myDB

我得到输出:

正常(1 毫秒)

如何将此输出存储在变量中以便测试是否正常?

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

但是,您说要查看结果。如果您存储该字符串输出,则需要解析它以仅查找 'OK'(因为 ping 时间可能不同),并处理横幅信息等(尽管这相对容易)。

tnsping 命令中查看 return 代码比查看其输出更简单:

tnsping myDB
echo $?

您可以测试$?的值。如果 ping 正常,那么它将为零;否则它将是 1.

你还没有说你想对测试结果做什么。如果你想显示错误(如果有的话)并且如果它有效则什么都不显示,你可以将两者结合起来:

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