Skip to content

Instantly share code, notes, and snippets.

@michaeltelford
Last active February 14, 2025 10:33
Show Gist options
  • Save michaeltelford/c50172100aaee27a4a190ee93d7cbc67 to your computer and use it in GitHub Desktop.
Save michaeltelford/c50172100aaee27a4a190ee93d7cbc67 to your computer and use it in GitHub Desktop.
Example bash script to awk and loop over STDOUT
#!/bin/bash
#
# Example bash script to capture, awk (filter) and loop over STDOUT.
#
key="some_key"
new_value="some_value"
echo "Updating crons to use new env key/value: $key=$new_value"
echo ""
# Grab some command STDOUT and pipe it into awk for filtering.
# 'NR>1 && NF=1' means grab every line bar the first (good for tables) && only the first column.
# 'while read cron' will loop over each line of STDOUT, storing that line in $cron.
kubectl get cj | awk 'NR>1 && NF=1' | while read cron; do
command="kubectl set env cj/$cron $key=$new_value"
echo "Updating env of cron: $cron"
echo "$command"
eval ${command}
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment