UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2003, 02:03 PM
colpaarm's Avatar
colpaarm colpaarm is offline
300lb Bench!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2001
Location: New York
Posts: 2,343 colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 10 h 50 m 4 sec
Reputation Power: 60
Don't understand simple for loop in shell script

I've taken over another programmer's code and need a little help with a simple shell script. The previous programmer wrote a script to descend down into a number of directories and make every file in the directory a symbolic link to a directory above it. There's one part, that although I'm positive is simple, I don't understand. Plus, I don't have a good resource for shell scripting, so I've come here. Here's the part the confuses me:

# The code cds into a directory a few lines before

for d in et00?
do
if [-d "$d" ]
then
echo Entering directory $d ...
cd $d
# rest of code

Based on where the shell script cds into, et001 et002 et003 et004 and et005 are five directories that match the et00?. There are other directories, but these would match if you were to create a regexp. Somehow this loop runs five times with $d equalling the directory names above on each pass.

This confuses me and I'll tell you why. I realize that ? is a wildcard and will match any character. However $d seems to somehow know that it's supposed to look at the directory structure and make matches on that. But it seems like steps are missing in the above code snippet. For example

for d in et00?
do
echo $d
done

prints out et001 et002 et003 et004 and et005. But what on earth is directing it to do a regexp match on the directories? Any help would be greatly appreciated.

Reply With Quote
  #2  
Old January 21st, 2003, 02:41 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
"for (d in ..." will always go for files/directories.

"if [-d "$d" ]" is the short form for:
if test -d "$d"
and "test" also refers to files/directories.

read the bash manual, it explains everything. in SuSE it is /usr/share/doc/packages/bash/bashref.html

for a simple example:
Code:
for i in *.txt; do
  echo $i
done

will output all ".txt" files
and:
Code:
for i in *; do
  if [ -d "$i" ]; then
    echo "$i is a folder."
  else
    if [ -x "$i" ]; then
      echo "$i is executable"
    else
      if [ -f "$i" ]; then
        echo "$i is a file."
      fi
    fi
  fi
done

you should now be able to guess...
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old January 21st, 2003, 07:15 PM
colpaarm's Avatar
colpaarm colpaarm is offline
300lb Bench!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2001
Location: New York
Posts: 2,343 colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 10 h 50 m 4 sec
Reputation Power: 60
Ok, I'll read the bash manual, as you suggested. It seems a little foreign to me, however. To me, I always expect for loops to do what for loops do, which is iterate through a sequence of things that you specify. As you've explained it, it almost seems as though for loops in shell programming are tailor made just to look through directories. Again, I'll start reading documentation and educate myself. Thanks.

Reply With Quote
  #4  
Old January 22nd, 2003, 01:00 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
itīs not the "for" loop. it does what you said.
it is the "*" and "?" that always expand to file names and the "test" function. the bash manual chapter about this is "substitution" and iirc the paragraph "filename substitution".

Reply With Quote
  #5  
Old January 22nd, 2003, 07:34 AM
colpaarm's Avatar
colpaarm colpaarm is offline
300lb Bench!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2001
Location: New York
Posts: 2,343 colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level)colpaarm User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 10 h 50 m 4 sec
Reputation Power: 60
Thanks for the info!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Don't understand simple for loop in shell script

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap