#!/bin/sh # split data from stdin to multiple invocations of a command # without creating temporary files # License: LGPLv2 # Author: # http://www.pixelbeat.org/ # Notes: # This script may be useful for chunking a stream before # sending to an FTP server supporting limited file sizes for e.g. # Using `ifne` from moreutils is not possible as it consumes BUFSIZ, # and doesn't indicate when no more data is available. I.E. it's good # for conditional _single_ invocations of a command only. # Changes: # V0.1, 29 Nov 2010, Initial release # http://github.com/pixelb/scripts/commits/master/scripts/split_to if test "$Argc" -lt 1 { setglobal me = $[basename $0] echo "Usage: $me size [-n] [command]" > !2 echo "split data from stdin to multiple invocations of a command" > !2 echo > !2 echo " -n pass the chunk number as the first parameter to command" > !2 echo "Examples:" > !2 echo " $0 10000 'wc -c' < /bin/ls" > !2 echo " $0 10000 < /bin/ls" > !2 echo " $0 10000 -n < /bin/ls" > !2 exit 1 } setglobal chunk_size = $1; shift test $1 = -n && do { setglobal pass_part = '1'; shift; } test $1 && setglobal cmd = "$ifsjoin(Argv)" || setglobal cmd = 'example_cmd' proc example_cmd { echo "processing part $1 ($[wc -c] bytes)"; } setglobal part = '1' while true { setglobal c = $[od -to1 -An -N1] # getc test $c || break setglobal c = $[echo $c] # trim test $pass_part && setglobal ppart = $part do { printf "\\$c" # ungetc head -c$shExpr('$chunk_size-1') } | $cmd $ppart || break setglobal part = $shExpr('$part+1') }