Pipe & Arguments
Example processing pipe line by line
Test case
foo() {
if [ -t 0 ]; then
echo "Called normally"
echo "Received arg: $1"
else
echo.green "Called from a pipeline"
while IFS= read -r line; do
echo "Processing piped input: $line"
done
fi
}
export -f foo
main() {
echo "Calling foo() normally"
foo "bar"
echo "--------------------------------------------------------------------------------"
echo "Calling foo() from a pipeline"
echo "bar" | foo
}
main "${@}" || exit 1
Output:
Calling foo() normally
Called normally
Received arg: bar
--------------------------------------------------------------------------------
Calling foo() from a pipeline
Called from a pipeline
Processing piped input: bar
real 0m0.016s
user 0m0.013s
sys 0m0.004s
Compress piped input with JQ
# shellcheck disable=SC2120
bar(){
if [ -t 0 ]; then
echo "BAR Called normally: $*"
else
echo "BAR Called from a pipeline"
while IFS= read -r line; do
echo.bold "BAR Processing piped input: $line"
sleep 2
done
fi
}
foo() {
if [ -t 0 ]; then
echo "${*:?}" | jq -c | bar
else
jq -c | bar
fi
}
export -f foo
main() {
echo.green "$(date.now)"
echo.jsonl | jq | foo
echo.green "$(date.now)"
foo '{"hi":"there"}'
}
main "${@}" || exit 1
Output:
2023-12-14T21-17-51PST
BAR Called from a pipeline
BAR Processing piped input: {"title":"title-val-1","note":"note-val-1"}
BAR Processing piped input: {"title":"title-val-2","note":"note-val-2"}
BAR Processing piped input: {"title":"title-val-3","note":"note-val-3"}
2023-12-14T21-17-57PST
BAR Called from a pipeline
BAR Processing piped input: {"hi":"there"}
Backlinks