Okay this is about my fifth thread on the topic because I’m balls-out lost in every aspect of this horrendous assignment. I’ve used other solutions that worked perfectly but cannot be employed such as using the awk command, sed, and comm. Instead, gotta do this the hard way.
I am so lost, at this point I’m not even doing it to hand it in because it’s a zero since it’s past the due date, I’ve been tearing my hair out for the past 18 hours trying to get this done just for the sake of getting it done. Much appreciated if you can save me from putting a bullet into my head.
Point of the assignment: You are to write a Bash shell script which will help compare the contents of two directories. Write a utility that satisfies the following requirements:
- Your script will compare filenames
in two directories, and list
information about filenames that are
in one directory but not the other.
The information listed will be a
long listing of each file, similar
to the “ls -l” command. The
directory names must be specified,
including any required absolute or
relative paths. - Your script will work for all kinds
of files, including directories that
are contained in the specified
directories. The script will print
an appropriate error message if the
number of arguments passed to it is
something other than 2, or if the
directory names specified are not
names of valid existing directories.
If an error message is issued, then
the script should end with an exit
status of 1 (one). Otherwise, it
should end with an exit status of 0
(zero).
Specifications:
- Files in the directory that this
script is supposed to work on don’t
have execute permissions, only read
which if I’m not mistaken can only
display file names, so we can’t see
file contents. I tried, I get
permission denied. - All information regarding this
assignment can be found here:
https://cs.senecac.on.ca/~lczegel/BTO120/assign1/assign1.html
What I have done:
#!/bin/bash
if [ ! -d $1 ]
then
echo $1 is not a valid existing directory >&2
exit 1
elif [ ! -d $2 ]
then
echo $2 is not a valid existing directory >&2
exit 1
elif [ $# = 0 ]
then
echo Usage: compdir dir-name1 dir-name2 >&2
exit 1
elif [ $# = 1 ]
then
echo Usage: compdir dir-name1 dir-name2 >&2
exit 1
elif [ $# = 2 ]
then
ls -a $1 > temp1
ls -a $2 > temp2
cat temp1 |
while read input
do
grep -Fvf temp1 temp2 > temp1_diff
done
cat ~temp2 |
while read input
do
grep -Fvf temp2 temp1 > temp2_diff
done
#Files that are in $1 but not in $2
cat temp1_diff |
while read input
do
Files that are in $1 but not in $2
cd $2
ls -la `cat ../temp1_diff`
done
cd ..
echo -e
#Files that are in $2 but not in $1
cat temp2_diff |
while read input
do
Files that are in $2 but not in $1
cd $1
ls -la `cat ../temp2_diff`
done
elif [ $# = 3 ]
then
echo Usage: compdir dir-name1 dir-name2 >&2
exit 1
else
echo Usage: compdir dir-name1 dir-name2 >&2
exit 1
fi
The problems I am encountering:
- It will not write any files that
have any characters like dots or
spaces or anything in them to
temp1_diff and temp2_diff. - I cannot get the echo to echo once
even if it’s before or in the loop. - I get constant errors that it cannot
navigate to $1 or $2 etc.
Once upon a time, there was a program called
dircmpthat did quite a lot of what is requested – the main difference being that it did not dols -lformat listings for files found in but not the other directory. That will require some hacking. Some time ago it became clear thatdircmpwas going the way of dinosaurs, but there’s enough of the palæontologist in to create a version that works for me. It will likely give you some issues if you have to deal with filenames containing blanks, tabs or newlines, but otherwise is likely to usable.Here is the not complete answer to your assignment, but at least a workable start point.