Warm tip: This article is reproduced from serverfault.com, please click

shell-Bash子串比较不适用于命令表达式

(shell - Bash substring comparison doesn't work for command expression)

发布于 2020-12-02 08:30:57

我正在尝试比较bash中的子字符串(如何检查字符串是否在Bash中包含子字符串)。通常我可以这样做:

if [[ "sdfdsfOpenSSHdsfsdf" == *"OpenSSH"* ]]; then echo "substring found"; fi

但是,当使用交互式命令时,它不起作用:

if [[ $(ssh -V) == *"OpenSSH"* ]]; then echo "substring found"; fi

的输出ssh -VOpenSSH_8.4p1, OpenSSL 1.1.1h 22 Sep 2020,因此我希望子字符串能够匹配。我想念什么?

Questioner
Kyu96
Viewed
0
Chris Maes 2020-12-02 16:33:55

ssh -Vgo的输出发送到stderr,你需要重定向到stdout以捕获它:

ssh -V 2>&1

因此,以下工作原理:

if [[ $(ssh -V 2>&1) == *"OpenSSH"* ]]; then echo "substring found"; fi