#!/bin/sh USAGE='Usage split.sh [options] filename\nOptions :\n -c N\tsplit filename in N files (default 2)\n -o O\tprefixe output files with O (default filename plus dot)\n -s N\tuse N-length suffixes (default 1) ' # Need at least the filename if [ $# -lt 1 ]; then echo -e $USAGE; exit 1 fi # Fetch options while getopts ":c:o:s::" optname do case "$optname" in "c") COUNT=$OPTARG ;; "o") PREFIXE=$OPTARG ;; "s") SUF_LENGTH=$OPTARG ;; "?") echo -e "Unknown option $OPTARG\n"$USAGE exit 2 ;; ":") echo -e "No argument value for option $OPTARG\n"$USAGE exit 1 ;; *) echo "Unknown error while processing options" exit 127 ;; esac done # Check that filename is present if [ $OPTIND -gt $# ]; then echo -e "filename missing\n"$USAGE exit 5 fi # and alone if [ $# -gt $OPTIND ]; then echo -e "too much arguments\n"$USAGE exit 6 fi FILE=${!OPTIND} # Filename is there but does it exist? if [ ! -r "$FILE" ]; then echo -e "file $FILE does not exists or is not readable" exit 7 fi # Set default values for unset options if [ "$COUNT" = "" ]; then COUNT=2 fi if [ "$PREFIXE" = "" ]; then PREFIXE="$FILE." fi if [ "$SUF_LENGTH" = "" ]; then SUF_LENGTH=1 fi TOTAL_LINES=`wc -l $FILE |cut -d" " -f1` LINES=`echo $TOTAL_LINES / $COUNT + 1| bc` split -l $LINES -d -a $SUF_LENGTH $FILE $PREFIXE