我正在尝试比较bash中的子字符串(如何检查字符串是否在Bash中包含子字符串)。通常我可以这样做:
if [[ "sdfdsfOpenSSHdsfsdf" == *"OpenSSH"* ]]; then echo "substring found"; fi
但是,当使用交互式命令时,它不起作用:
if [[ $(ssh -V) == *"OpenSSH"* ]]; then echo "substring found"; fi
的输出ssh -V
是OpenSSH_8.4p1, OpenSSL 1.1.1h 22 Sep 2020
,因此我希望子字符串能够匹配。我想念什么?
将ssh -V
go的输出发送到stderr,你需要重定向到stdout
以捕获它:
ssh -V 2>&1
因此,以下工作原理:
if [[ $(ssh -V 2>&1) == *"OpenSSH"* ]]; then echo "substring found"; fi
啊,那很合情理。不知道版本输出到stderr。谢谢!