如何在 Bash 中检测 OpenSSL 的故障

How to detect failure from OpenSSL in Bash

似乎 OpenSSL 在出现错误时不会 return 退出代码。例如,运行 不是 OpenSSL 命令(应该是 "rand"),但 shell 结果代码仍设置为零。有没有办法在这里更改 OpenSSL 的行为(除了构建自定义版本)?

例如,打印 "ok"、"ok",而不是预期的 "fail"、"ok":

openssl ran 8
if [ $? -eq 0 ]; then
   echo "ok"
else
   echo "fail"
fi
openssl rand 8
if [ $? -eq 0 ]; then
   echo "ok"
else
   echo "fail"
fi
openssl ran 8 2>/tmp/err
if [ -s /tmp/err ]
then
  echo fail
else
  echo ok
fi