Last active
July 2, 2024 21:18
-
-
Save pedramamini/db892ddeed5179a58309b7345970a864 to your computer and use it in GitHub Desktop.
Multiplex the stdout from a command as the stdin to numerous commands, collect the outputs under command headings in markdown format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Pedram Amini | |
# https://pedramamini.com | |
run_command() { | |
local cmd="$1" | |
local input_file="$2" | |
local output_file="$3" | |
{ | |
echo "# $cmd" | |
fabric --pattern "$cmd" < "$input_file" | |
echo # Add an empty line after each command's output | |
echo | |
} > "$output_file" | |
} | |
multiplex() { | |
local tmpfile=$(mktemp) # Temporary file for input | |
local output_files=() # Array to hold temporary files for outputs | |
cat > "$tmpfile" # Save the stdin to the temporary file | |
for cmd in "$@"; do | |
local output_file=$(mktemp) # Create a temporary file for this command's output | |
output_files+=("$output_file") # Store the output file in the array | |
run_command "$cmd" "$tmpfile" "$output_file" & | |
done | |
wait # Wait for all commands to complete | |
# Concatenate and display the outputs in the correct order | |
for output_file in "${output_files[@]}"; do | |
cat "$output_file" | |
rm "$output_file" # Clean up the output file | |
done | |
rm "$tmpfile" # Clean up the input temporary file | |
} | |
if [ "$#" -lt 1 ]; then | |
echo "Usage: $0 command1 [command2 ...]" | |
exit 1 | |
fi | |
multiplex "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment