#!/bin/bash
#
# Usage
#
usage ()
{
echo "$0 [-f|-r|-n <number>] source destination"
echo " -f : if not set the script won't overwrite an"
echo " existing destination file "
echo " -n <number> : set the number of blank lines to add must be"
echo " a strictly positive integer. This is optional"
echo " and default is 2"
exit 0
}
#
# Test whether the value is a strictly positive integer.
#
testNumber()
{
echo $1 | grep "^[1-9][0-9]*$" > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
return 0
else
return 1
fi
}
#=============================================================================
# First, let's initialize vars
zeERASE="n"
zeNUMBER=2
# Then read the options ...
while getopts :hfn: c
do
case $c in
h) usage $0
;;
f) zeERASE="y"
;;
n) zeNUMBER=$OPTARG
;;
\?) $ECHO "Unknown option [$c] : ignored."
usage $0
;;
esac
done
shift `expr $OPTIND - 1`
# ... and test them
if ! testNumber "$zeNUMBER" ; then
usage $0
fi
# Then check the parameters ...
if [ $# -lt 2 ]
then
usage $0
fi
zeINPUT=$1
zeOUTPUT=$2
# ... and test them
if [ ! -f $zeINPUT ]; then
printf "\nSource doesn't exist ...\n\n"
usage $0
fi
if [ -f $zeOUTPUT ]; then
if [ "$zeERASE" != "y" ]; then
printf "\nDestination already exists ; use '-f' to overwrite\n\n"
usage $0
fi
fi
# Now everything is defined, let's start !
zeCOUNT=0
while read line
do
if [ $zeCOUNT -ne 0 ]; then
for (( i=1 ; i <= $zeNUMBER ; i++ )); do
echo ""
done
else
zeCOUNT=1
fi
echo $line
done < $zeINPUT > $zeOUTPUT