Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-20-2011, 07:39 AM   #1
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 621

Rep: Reputation: 142Reputation: 142
bash loops not behaving

Hi, I maintain a legacy system written in pascal (popular language in 70s/80s for the younger folk) and the operating system is VMS. These are largely irrelevant but you never know!

Recent changes have not been well coordinated and I am concerned that some dev code did not make it into the production area (there is no source control). As I maintain things remotely I don't want to spend all day on a VPN checking the code.

I have copied both trees to my laptop (Fedora 14) and I want to write a script which will simply loop through all directories and dump md5sums of all pascal files. I can then compare the contents of the 2 trees for differences. A complication is that some directories have sub directories so I need to search these also.

I have pasted the script so far as I have a problem. The first "for i in *" looks at all files in the top level. I then check if it is a directory (" if [ -d..."). If true then move to that level and check for any directories in the next level. Then check the pascal files in the next level directory.

What I find is that the script finds the first directory and navigates down. When it returns to the top level, it treats the next directory as a normal file and skips it. It only recognises the first directory as such.

Aha, how about I rename the first directory to something like "zzz" so the script will look at the 2nd directory first! Same thing, the "if" statement only recognises one directory as such and skips the rest.

I have tried to explain my problem the best I can. Simply, are there any issues relating to repeated use of "if [ -d"?

Alternatively, is there a simple linux tool which I can use to compare files in 2 trees and highlight the differences.

#! /bin/sh
for i in *
echo 1 $i 5
if [ -d "$i" ]; then
echo 2 $i 5
cd $i
for j in *; do
if [ -d $j ]; then
cd $j
for k in *.pas; do
md5sum ${k}
md5sum ${k} >> csum
Old 07-20-2011, 08:15 AM   #2
Senior Member
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You should just be able to run:

diff dir1 dir2 -r
For me, this gives output like:

Only in dir1/A: file1
diff -r dir1/B/file2 dir2/B/file2
Only in dir2/C: file3
1 members found this post helpful.
Old 07-20-2011, 08:28 AM   #3
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,163

Rep: Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370Reputation: 1370
The problem with the bash script is that you cd'd into the subdirectory but then didn't go back up. But you are better off using diff for this.

PS please put code inside code tags, like this:
[code] your code here [/code]
Old 07-20-2011, 08:29 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Well diff would be the tool i would use to compare 2 files.

May I suggest as you are looking for files only that you use find to return all the files in one tree and then assuming the same format on the other structure simply compare.


while read -r PAS
    diff -q $PAS $start_of_second_tree/$PAS
done< <(find . -type f -name '*.pas')
So this assumes we are executing from with the top level of the first tree and should output something along the lines of:
Files ./first/tree/file1.pas and /second/tree/./first/tree/file1.pas differ
Well you get the idea.

Edit: hmmm .. or you could just do Snark's way ... live and learn

Last edited by grail; 07-20-2011 at 08:30 AM.
Old 07-20-2011, 08:39 AM   #5
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 621

Original Poster
Rep: Reputation: 142Reputation: 142
well thanks all for the rapid response:

snark1994 solution seems simple and efficient
ntubski thanks for the code review. Doh!
grail I am not sure exactly how your code works but it gives me something to investigate
Old 07-20-2011, 09:12 AM   #6
LQ Newbie
Registered: Nov 2010
Location: MN
Distribution: Fedora and RHEL
Posts: 20

Rep: Reputation: 2
diff -r may be what you are looking for

wow...didn't refresh and look how many responses beat me to the punch...

Last edited by minnarky; 07-20-2011 at 09:13 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash is behaving weird inside chroot ReyJavikVI Linux From Scratch 9 08-09-2009 08:48 PM
Bash behaving differently jxrod2000 Linux - Software 2 10-06-2008 05:58 PM
Bash Loops multiplex A/V lpn1160 Linux - General 1 02-23-2008 06:01 AM
Bash Loops UnitedWeFall Programming 8 08-21-2007 06:33 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration