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
Xseconds. - Read
Yamount (or less than Y if there isn't enough) data from pipe. - Write the read data to standard output.
Where:
Xcould be1..n.Ycould 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 dataconstruct, it filters all white characters in the encoding your using. - Using
dd bs=1 count=1and looping.dddoesn't seem to have different exit codes for when there were something inifand 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?