Skip to content

Instantly share code, notes, and snippets.

@luk707
Last active January 7, 2023 12:56
Show Gist options
  • Save luk707/721c5c986fde1bed6e0c0e82b937d553 to your computer and use it in GitHub Desktop.
Save luk707/721c5c986fde1bed6e0c0e82b937d553 to your computer and use it in GitHub Desktop.
Concurrently run commands with output prefix
#!/bin/sh
colors=("\033[0;31m" "\033[0;32m" "\033[0;33m" "\033[0;34m" "\033[0;35m" "\033[0;36m")
RESULT=0
p=0
trap "kill 0" EXIT
while [[ $# -gt 0 ]]; do
key="$1"
if [[ $1 == -* ]]
then
$2 2>&1 | awk -v prefix="[${colors[p % ${#colors[@]}]}${key#?}\033[0m] " '{print prefix $0}' &
shift
else
$1 2>&1 | awk -v prefix="[${colors[p % ${#colors[@]}]}$1\033[0m] " '{print prefix $0}' &
fi
p=$((p+1))
shift
done
for job in `jobs -p`
do
wait $job || let "RESULT=1"
done
@luk707
Copy link
Author

luk707 commented Jan 6, 2023

Usage: cxy -test "echo \"hello\""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment