Trying to figure out how to have a shell script only take fix-length blocks out of stdin
. I would have thought something like this would have worked, but it does not:
#!/bin/bash
value=0
while [ "$value" != "-1" ]; do
read -r -d '' -n 20 value
if [ "$value" != "-1" ]; then
dd conv=notrunc status=none of=/path/bigfile.bin bs=1M count=1 seek=$value
fi
done
In a nutshell I am trying to copy specific blocks from a large file to the same file a remote location. The data sent from the sending script has a block location (20 bytes) followed by a 1 MiB of the data to be written to that location. It is finished when the location is -1.
I have a setup that works fine if running a compiled C program, but I'd like to avoid this and run with the native shell commands. The problem is, it doesn't seem that dd
consumes any data, and all of it is handled by the read
command.
Can stdin
be consumed by multiple commands?
stdin
could be consumed by many programs, but you'd need to coordinate that very well. If you go down that road, it might make sense to convert the 1M data to some ASCII representation, send it over and then convert it back. By the way, it might be easier to help you if you post the other side of the pipe.rsync
. What's the true goal - for example, is it to make the remote file the same as the local one by sending as little data as possible?rsync
, but rsync (as far as I know) only works on entire files. If it sees a 1 TB file has changed, it will simply send the 1 TB file--not just the parts that have changed. My goal is to only send the parts that have changed. Since I don't expect a lot of change between synchronizations, this incremental method is preferable. I'm not sure such a setup is useful outside a narrow use case--hence the specific question.