Last active
March 26, 2021 20:00
-
-
Save SomajitDey/86ac2cd78e2acb66173402da86a5fbe0 to your computer and use it in GitHub Desktop.
Dump input stream at a given path
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
# This function dumps the input stream to the path given as parameter. | |
# If PATH gets unlinked by another (cooperative) process, it creates a new file at PATH and continues dump | |
# Provide a lockfile as the 2nd positional parameter. This is needed so that another process doesn't remove PATH | |
# when it is being written to. | |
stream_dump(){ | |
declare -x path="${1}" | |
local lock="${2}" | |
local timeout="1" # Interval for polling $path | |
declare -x line | |
IFS= | |
while :; do | |
read -r -d '' -t "${timeout}" line | |
flock "${lock}" -c 'echo -n "${line}" >> "${path}"' | |
done | |
} | |
# Test: | |
# Source this function with: . ./dump.bash | |
# Keep typing input stream after: stream_dump my_dump.txt write.lock | |
# In another terminal run: tail --quiet -F my_dump.txt 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment