首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ipset命令的输出不存储到变量

ipset命令的输出不存储到变量
EN

Unix & Linux用户
提问于 2018-09-23 21:04:02
回答 1查看 344关注 0票数 0

我正在构建一个脚本来检测IPSET是否存在。

代码语言:javascript
复制
#!/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命令输出存储到变量?

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2018-09-24 01:56:30

感谢@StephenHarris

ipset命令的输出是在stderr (不是stdout)上生成的,2>&1将输出捕获到变量。

代码语言:javascript
复制
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语句可以像预期的那样工作!

票数 1
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/470939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档