Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 01-19-2011, 10:52 AM   #1
LQ Newbie
Registered: Jan 2011
Posts: 12

Rep: Reputation: 0
Shell Script - Use variable in a for loop with directory path

I modified files in several directories, and need to run a diff on the backup I created before modifying the file.

I'm trying to compose a simple shell script to speed up the task, but getting a syntax error.

Here is what I have:

for i in DIR1 DIR2 DIR3 DIR4 DIR5 do;
diff /maindir/subdir/subdir/$i/filename.txt.old /maindir/subdir/subdir/$i/filename.txt;

I know the paths are valid, and if I run just the diff command with the actual DIR1 instead of $i it works.

Any suggestions as to what I'm missing?

Thanks in advance.
Old 01-19-2011, 10:57 AM   #2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 614Reputation: 614Reputation: 614Reputation: 614Reputation: 614Reputation: 614
Posting the exact error text you get would help..

You have a misplaced semicolon.
for i in DIR1 DIR2 DIR3 DIR4 DIR5; do
diff /maindir/subdir/subdir/$i/filename.txt.old /maindir/subdir/subdir/$i/filename.txt;
1 members found this post helpful.
Old 01-19-2011, 11:22 AM   #3
LQ Newbie
Registered: Jan 2011
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks much! I was initially entering this line by line and thought the problem was with using the variable in the middle of a path within the script.

Of course, as usual, when I am banging my head against the wall trying to figure out why something isn't working...turns out to be a simple oversight/sytnax error/typo!


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 06:48 PM
shell script question $variable in loop icecubeflower Linux - Newbie 2 03-31-2009 10:09 AM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 04:07 AM
Shell variable in Linux is reset after while loop CBenner Programming 7 05-19-2006 10:46 AM
Need shell script to concatenate a string and a variable into a directory name AwesomeMachine Linux - Newbie 2 05-07-2006 04:42 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:31 PM.

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