Here is a pure bash shell script to compare files:
#!/usr/bin/env bash
# @(#) s1 Demonstrate rudimentary diff using shell only.
# Infrastructure details, environment, debug commands for forum posts.
# Uncomment export command to run as external user: not context, pass-fail.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f "$C" ] && $C
set -o nounset
FILE1=${1-data1}
shift
FILE2=${1-data2}
# Display samples of data files.
pl " Data files:"
head "$FILE1" "$FILE2"
# Set file descriptors.
exec 3<"$FILE1"
exec 4<"$FILE2"
# Code based on:
# http://www.linuxjournal.com/content/reading-multiple-files-bash
# Section 2, solution.
pl " Results:"
eof1=0
eof2=0
count1=0
count2=0
while [[ $eof1 -eq 0 || $eof2 -eq 0 ]]
do
if read a <&3; then
let count1++
# printf "%s, line %d: %s\n" $FILE1 $count1 "$a"
else
eof1=1
fi
if read b <&4; then
let count2++
# printf "%s, line %d: %s\n" $FILE2 $count2 "$b"
else
eof2=1
fi
if [ "$a" != "$b" ]
then
echo " File $FILE1 and $FILE2 differ at lines $count1, $count2:"
pe "$a"
pe "$b"
# exit 1
fi
done
exit 0
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.9 (jessie)
bash GNU bash 4.3.30
-----
Data files:
==> data1 <==
I need to buy apples.
I need to run the laundry.
I need to wash the dog.
I need to get the car detailed.
==> data2 <==
I need to buy apples.
I need to do the laundry.
I need to wash the car.
I need to get the dog detailed.
-----
Results:
File data1 and data2 differ at lines 2, 2:
I need to run the laundry.
I need to do the laundry.
File data1 and data2 differ at lines 3, 3:
I need to wash the dog.
I need to wash the car.
File data1 and data2 differ at lines 4, 4:
I need to get the car detailed.
I need to get the dog detailed.
The comments on specific commands can be removed to exit at the first difference seen, and if you desire to see every line that is read.
See page at http://www.linuxjournal.com/content/reading-multiple-files-bash for details on file descriptors such as "&3".
Best wishes ... cheers, drl
diff
command? – Romeo Ninov Oct 12 '17 at 10:04