Hey, I started bash shell scripting and I'm trying to make a script for an assignment that when you enter two directories, it will check if they exist and display according error message and if both directories DO exist, it will list the differences between the current directories.
$ cd dir-1
$ myshellscript . dir-2 (comparing . aka dir-1 against dir-2)
Output:
Files that are in . but not in dir-2
-rw------- 1 ddddd users 1 2011-03-1 01:26 123123123
Files that are in dir-2 but not in .
-rw------- 1 ddddd users 1 2011-03-1 01:26 zzzzzzzzzzzz
What I have so far that does not seem to detect whether a directory exists nor list differences:
dir-1=$1
dir-2=$2
if [ $# > 2 ]
then
echo "Usage: compdir dir-name1 dir-name 2"
exit 1
elif [ $# < 2 ]
then
echo "Usage: comdir dir-name1 dir-name 2"
elif [ ! -d "$@" ]
then
echo "/$@ is not a valid existing directory"
else
exit 0
fi
echo $dir-1
echo $dir-2
List of commands I have to work with, otherwise I would have used comm -32 <(ls -la dir-1)
<(ls -la dir-2)
http://dl.dropbox.com/u/20930447/index.html
That's one half of it.
Replace
[[ ... ]]
by[ ... ]
ortest ...
if not using Bash.This almost works. It mainly fails where there are files that are similar locations alphabetically between the two dirs.
The basic recipe of what you want to do, is already done using the
diff
utility available on unix-like systems, or using cygwin or GnuWin on Windows. You should exploit this fact.If I have directory
a
andb
with the following contents:The
x
,y
, andz
are exactly the same in each directory.I can achieve what you want using the
diff
command like this:If I add a new file to each directory (named
new
), which are different, I get the following:That is, it'll even tell you how, and where the differences in the files occur. Of course, if you don't want or need this functionality, you're free to not use it.
You also get the following:
With the heavy-lifting of this program done by diff, most of what you write will be parsing the output of this command, and then manipulating or outputting it as you see fit.
One of
awk
orsed
might be of particular interest when you're doing this.a bit crude - but the easiest way I always use is (can play with the diff params, I typically use different grep
then you can sort and format as you like
Revised to format (less efficient as we are running diff twice here ... easily solved)
Expanding on the sed expression above:
s=search and replace
the three '@' are separating the expressions (this is TRADITIONALLY done with '/')
^ matches the beginning of a line (forces the rest not to match elsewhere) . means any character
* means the previous expression (.==match any char) 0-N times ": " is what I matched on from the diff output "Only in X: "
Look Mommy, no hands - now without 'sed' its beginning to be less and less crude
You will probably want to know about IFS ... it needs some reading in the bash manual, but its basically the field separator characters ... by default they include spaces and I don't want the loop to be fed with fractions of lines, just complete lines - so for the duration of the loop I override the default IFS to just newlines and carriage returns.
BTW maybe your professor is reading stackoverflow, maybe next you wont be allowed to use semicolons ;-) ... (back to 'man bash' ... BTW if you do 'man bash' do it in emacs, makes much easier to read IMO)
I like to use diff for comparing:
You could also analyze it with read:
Proof of Concept