I have been trying for about an hour now to find an elegant solution to this problem. My goal is basically to write a bandwidth control pipe command which I could re-use in various situations (not just for network transfers, I know about scp -l 1234). What I would like to do is:
- Delay for
X
seconds. - Read
Y
amount (or less than Y if there isn't enough) data from pipe. - Write the read data to standard output.
Where:
X
could be1..n
.Y
could be 1 Byte up to some high value.
My problem is:
- It must support binary data which Bash can't handle well.
Roads I've taken or at least thought of:
- Using a
while read data
construct, it filters all white characters in the encoding your using. - Using
dd bs=1 count=1
and looping.dd
doesn't seem to have different exit codes for when there were something inif
and not. Which makes it harder to know when to stop looping. This method should work if I redirect standard error to a temporary file, read it to check if something was transfered (as it's in the statistics printed on stderr) and repeat. But I suspect that it's extremely slow if used on large amounts of data and if it's possible I'd like to skip creating any temporary files.
Any ideas or suggestions on how to solve this as cleanly as possible using Bash?