how to store large output in a variable in bash script

4/9/2021

whenever i hit this command $APIGEECTL_HOME/apigeectl check-ready -f overrides/overrides.yaml it will give me logs like this Error: ready check failed: one or more pods is not ready: [{"namespace":"apigee","pod":"apigee-cassandra-default-0","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-cassandra-schema-setup-seraphic-spider-45752e3-qqkdp","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-cassandra-user-setup-seraphic-spider-45752e3-6nblk","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-logger-apigee-telemetry-gwzc4","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-mart-seraphic-spider-45752e3-142-35llw-54v2g","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-metrics-apigee-telemetry-app-142-k3l39-phct6","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-metrics-apigee-telemetry-proxy-142-kgp0o-dtfz9","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-runtime-seraphic-spider-test-252850d-142-rwjcy-f7gsx","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-synchronizer-seraphic-spider-test-252850d-142-dfe1rld2qw","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-udca-seraphic-spider-test-252850d-142-7acpg-cl2r9","reason":"ContainersNotReady","status":"False"},{"namespace":"apigee","pod":"apigee-watcher-seraphic-spider-45752e3-142-r9pgy-8r6c2","reason":"ContainersNotReady","status":"False"}] it`s big logs is shown only pods are not ready they are trying to be ready so i want to store this logs in a variable and run a loop like when all pods are created then this command show a message like "All pods are ready" means whenever command got this message loop will be terminate

-- user15224751
bash
kubernetes
shell
variables

1 Answer

4/9/2021

To catch the standard output of a program, do something like

pout=$(prog)

To catch the standard error of a prorgram, do

perr=$(prog 2>&1 >/dev/null)

To catch both into different variables, do

errout=tmp.$
pout=$(prog 2>$errout)
perr=$(<$errout)
rm $errout

This gives you the respective output as a scalar. If you want to have it in an array, you do something like

pout=( $(prog) )

etc.

-- user1934428
Source: StackOverflow