【问题标题】:Bash script to read output of 'openssl help'用于读取“openssl 帮助”输出的 Bash 脚本
【发布时间】:2017-06-21 15:11:24
【问题描述】:

我尝试读取 bash 命令“openssl help”的输出并将其放入字符串变量中以供进一步处理。

确切地说,我想测试所有 Cipher 命令。

首先,我尝试读取命令“openssl ciphers”的输出。但我只有密码而不是密码命令。但是,如果我键入“openssl help”,则会显示密码命令。问题是现在命令的输出没有保存在我的变量中。

CIPHER=`openssl ciphers`
echo "Output:"
echo $CIPHER

这行得通。但不幸的是 $CIPHER 的内容不是我需要的。

CIPHER=`openssl help`
echo "Output:"
echo $CIPHER

这不起作用。变量 CIPHER 为空。为什么??

【问题讨论】:

  • 你想要openssl ciphers还是openssl help

标签: string bash openssl


【解决方案1】:

似乎openssl help 的内容被写入标准错误流stderr(2) 而不是stdout(1)。建议将错误流(由文件描述符2表示)重新重定向到标准输出(由文件描述符1表示)以解决问题。

由于输出包含多行流,建议使用适当的双引号将 $(..) 命令替换放在反引号(过时的技术)上。

sslOutput="$(openssl help 2>&1)"
printf "%s\n" "${sslOutput}"

看看为什么$(..) is prefereed over legacy .. syntax for command substitution.

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
相关资源
最近更新 更多