The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Simple for loop question arex876 Shell Programming and Scripting 7 07-23-2008 03:59 PM
while loop question stepnkev Shell Programming and Scripting 2 06-01-2007 09:51 AM
oracle and loop question pavan_test UNIX for Dummies Questions & Answers 4 09-28-2006 11:23 PM
Simple while loop question Brokeback Shell Programming and Scripting 3 07-21-2006 09:04 AM
Loop question strike Shell Programming and Scripting 18 04-13-2005 10:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
For Loop Question

Hi, I am writing a bash shell script. I will have a section of code devoted to a for loop:

for FILE in *.hdf *.L2; do
echo hello world
done

The complication is that sometimes there will not be any files with a ".hdf" extension, or sometimes there won't be any with a ".L2" extension. Since there are files with other extensions I can't just say "for FILE in *; do". How can I make my script loop through a number of times equal to the amount of files ending in .hdf or .L2, but not care if sometimes either of those filetypes doesn't exist?

Mike
  #2 (permalink)  
Old 08-26-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
What you have should work, just add the nullglob option.

Code:
set -o nullglob
  #3 (permalink)  
Old 08-26-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
Hi era,

Thanks for the reply. Where in the code is that option set, and when does it stop taking effect?
  #4 (permalink)  
Old 08-26-2008
deaconf19 deaconf19 is offline
Registered User
  
 

Join Date: Sep 2007
Location: EX-PAT in Taipei, Taiwan
Posts: 152
place the set -o nullglob at the beginning of your code

Code:
 set -o nullglob
Code:
 for FILE in *.hdf *.L2; do
  #5 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The option is in effect until you change it again or until the current shell exits. In a shell script, the "current shell" exits when the script ends.
Sponsored Links
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0