[alt.sources] Simple binary split

tml@hemuli.atk.vtt.fi (Tor Lillqvist) (06/03/89)

I needed a binary file split, and didn't have any available, so I
wrote this simple script.  I hope it is of use to someone.

#!/bin/sh

# Binary file splitter

myname=`basename $0`
usage="Usage: $myname [ -n chunksize ] [ file [ name ] ]"

set -- `getopt n: $*`

if [ $? != 0 ]; then
	echo $usage
	exit 2
fi

chunksize=100000
namebase=x

for i in $*; do
	case $i in
	-n)	chunksize=$2;
		case $chunksize in
		[1-9]*)	;;
		*)	echo $usage
			exit 2;;
		esac
		shift 2;;
	--)	shift; break;;
	esac
done

if [ $# -gt 2 ]; then
	echo $usage
	exit 2
fi

if [ $# -gt 0 ]; then
	if [ $1 = - ]; then
		: standard input
	else
		namebase=$1
		exec <$1
	fi
fi

if [ $# = 2 ]; then
	namebase=$2
fi

alphabet=abcdefghijklmnopqrstuvwxyz
ext1=a
ext2=a

while :; do
	ext=$ext1$ext2
	blks=`dd bs=$chunksize count=1 of=$namebase.$ext 2>&1`
	case "$blks" in
	0+0*)	rm $namebase.$ext
		exit;;
	esac
	if [ $ext2 = z ]; then
		i=`expr 1 + index $alphabet $ext1`
		ext1=`expr substr $alphabet $i 1`
		ext2=a
	else
		i=`expr 1 + index $alphabet $ext2`
		ext2=`expr substr $alphabet $i 1`
	fi
done
-- 
Tor Lillqvist
Technical Research Centre of Finland, Computing Services (VTT/ATK)
tml@hemuli.atk.vtt.fi [130.188.52.2]