I have the following Bash script,
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Error: No arguments supplied. Please provide two files."
exit 100
elif [ $# -lt 2 ]
then
echo "Error: You provided only one file. Please provide exactly two files."
exit 101
elif [ $# -gt 2 ]
then
echo "Error: You provided more than two files. Please provide exactly two files."
exit 102
else
file1="$1"
file2="$2"
fi
if [ $(wc -l "$file1" | awk -F' ' '{print $1}') -ne $(wc -l "$file2" | awk -F' ' '{print $1}') ]
then
echo "Error: Files $file1 and $file2 should have had the same number of entries."
exit 200
else
entriesNum=$(wc -l "$file1" | awk -F' ' '{print $1}')
fi
for entry in $(seq $entriesNum)
do
path1=$(head -n$entry "$file1" | tail -n1)
path2=$(head -n$entry "$file2" | tail -n1)
diff "$path1" "$path2"
if [ $? -ne 0 ]
then
echo "Error: $path1 and $path2 do not much."
exit 300
fi
done
echo "All files in both file lists match 100%."
done
which I execute giving two file paths as arguments:
./compare2files.sh /path/to/my\ first\ file\ list.txt /path/to/my\ second\ file\ list.txt
As you can see, the names of the above two files contain spaces, and every file itself contain a list of other file paths, which I want to compare line by line, e.g the first line of the one file with the first of the other, the second with the second, and so on.
The paths listed in the above two files contain spaces too, but I have escaped them using backslaces. For example, file /Volumes/WD/backup photos temp/myPhoto.jpg is turned to /Volumes/WD/backup\ photos\ temp/myPhoto.jpg.
The problem is that script fails at diff command:
diff: /Volumes/WD/backup\ photos\ temp/myPhoto.jpg: No such file or directory
diff: /Volumes/WD/backup\ photos\ 2022/IMG_20220326_1000.jpg: No such file or directory
Error: /Volumes/WD/backup\ photos\ temp/myPhoto.jpg and /Volumes/WD/backup\ photos\ 2022/IMG_20220326_1000.jpg do not much.
When I modify the diff code like diff $path1 $path2 (without double quotes), I get another kind of error:
diff: extra operand \`temp\'
diff: Try `diff --help' for more information
Error: /Volumes/WD/backup\ photos\ temp/myPhoto.jpg and /Volumes/WD/backup\ photos\ 2022/IMG_20220326_1000.jpg do not much.
Apparently the files exist and the paths are valid, but the spaces in paths' names are not handled right. What is wrong with my code and how can be fixed (apart from renaming directories and files)?