#!/bin/bash
book=$1
[[ -z $book ]] && { echo “No argument supplied, exiting!”; exit 1; }
[[ ! -e $book ]] && { echo “Can’t find a file with the name $book, exiting”; exit 1; }
title=${book%.*}
bookcounter=0
linecounter=0
sed -i ‘s/\r$//’ “$book”
while read line
do
((linecounter+=1))
if [[ $linecounter -gt 300 && -z “$line” || -z “$output_file” ]]; then
linecounter=0
((bookcounter+=1))
formatted_bookcounter=$(printf “%03d” $bookcounter)
output_file=”${title}${formatted_bookcounter}.txt”
echo “…starting segment $output_file”
echo “$title – segment $formatted_bookcounter” > $output_file
echo “===================” >> $output_file
echo “” >> $output_file
fi
echo “$line” >> $output_file
done < "$book"