Reputation: 601
Just want to save the output from a kubectl command in a variable in a Bash script.
For example: command kubectl get all -n mattermost
outputs No resources found in mattermost namespace.
foo=$(kubectl get all -n mattermost)
while [ "$foo" = "No resources found in mattermost namespace." ]
do
echo "yay"
done
Unfortunately the output won't save in the variable..
Upvotes: 0
Views: 6133
Reputation: 1042
There are two problems with your script...
It should be more like ...
foo=$(ANY COMMAND 2>&1)
while [ "$(kubectl get all -n mattermost 2>&1)" = "No resources found in mattermost namespace." ]; do
echo "Hello World"
done
This way your loop will stop when stderr or stdout will change. There is also the possibility to stop the loop with a break
or stop the script with return
or exit
(depending on how you will run that script).
Upvotes: 2
Reputation: 158
For a message like "No resources found ....", it is printing in stderr. To rectify this, you can modify your line to
foo=$(kubectl get all -n mattermost 2>&1)
Upvotes: 2