我正在构建一个脚本来检测IPSET是否存在。
#!/bin/bash
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1)
echo "$str" #outputs blank line
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi当我运行这个脚本时,我得到了这个输出:
ipset v6.29: The set with the given name does not exist
然后是echo "$str"的空行,我看不到if语句的预期输出。
如何将ipset命令输出存储到变量?
发布于 2018-09-24 01:56:30
感谢@StephenHarris
ipset命令的输出是在stderr (不是stdout)上生成的,2>&1将输出捕获到变量。
str=$(/usr/sbin/ipset test IPsetName 1.1.1.1 2>&1)
if [[ $str = *"The set with the given name does not exist"* ]]; then
echo "IPsetName not found"
fi现在,这个if语句可以像预期的那样工作!
https://unix.stackexchange.com/questions/470939
复制相似问题