This next script reads a text file and creates a copy of it:
#!/bin/sh # # 6/1/2017 # echo "Chapter 7 - Script 5" if [ $# -ne 2 ] ; then echo "Usage: script5 infile outfile" echo " Copies text file infile to outfile." exit 255 fi INFILE=$1 OUTFILE=$2 if [ ! -f $INFILE ] ; then echo "Error: File $INFILE does not exist." exit 100 fi if [ $INFILE = $OUTFILE ] ; then echo "Error: Cannot copy to same file." exit 101 fi rm $OUTFILE 2> /dev/null # remove it echo "Reading file $INFILE ..." x=0 while IFS= read -r linevar # use read to put line into linevar do echo "$linevar" >> $OUTFILE # append to file let x++ done < $INFILE # the file to use as input echo "$x lines read." diff $INFILE $OUTFILE # use diff to check the output rc=$? if [ $rc -ne 0 ] ; then echo "Error, files do not match." exit 103 else echo "File $OUTFILE created." fi sum $INFILE $OUTFILE # show the checksums echo "End of script5" exit $rc
Here is the screenshot for Script 5:

This shows how to read and write a text file in a script. The following explains each line:
Usage message is displayed.100 if it does not.while loop reads and writes the lines. A count is made of the number of lines in x.diff command is used to make sure the files are the same.sum command is run on the two files.