首页 > 解决方案 > 将 kubectl 命令输出保存在 Bashscript 的变量中

问题描述

只想将 kubectl 命令的输出保存在 Bash 脚本的变量中。

例如:命令kubectl get all -n mattermost输出No resources found in mattermost namespace.

foo=$(kubectl get all -n mattermost)

while [ "$foo" = "No resources found in mattermost namespace." ]
do
  echo "yay"
done

不幸的是,输出不会保存在变量中..

标签: bashshellkubectl

解决方案


对于像“找不到资源......”这样的消息,它正在 stderr 中打印。要纠正这个问题,您可以将您的行修改为

foo=$(kubectl get all -n mattermost 2>&1)

推荐阅读