Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Last active October 2, 2024 08:53
Show Gist options
  • Save iolloyd/8e0be994e1396b21d9ac9f0d19cd8d58 to your computer and use it in GitHub Desktop.
Save iolloyd/8e0be994e1396b21d9ac9f0d19cd8d58 to your computer and use it in GitHub Desktop.
file_or_pipe
#!/bin/bash
# Function to process input (file or stdin)
process_input() {
while IFS= read -r line; do
# Perform any processing here
echo "$line"
done
}
# Main logic
if [ $# -gt 0 ]; then
# If arguments are provided, process each file
for file in "$@"; do
if [ -f "$file" ]; then
process_input < "$file"
else
echo "Error: File '$file' does not exist." >&2
fi
done
else
# If no arguments, process stdin
process_input
fi
@iolloyd
Copy link
Author

iolloyd commented Oct 2, 2024

removed duplication of read block

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