Register
It is currently Thu Sep 18, 2014 9:39 am

Execute script for all config files in a folder?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Nov 05, 2008 6:50 am   

Joined: Wed Nov 05, 2008 6:19 am
Posts: 7
Hi all,

A simple one, I think, but I'm not sure where to start...

I have a folder with a bunch of .conf files in it, and I want to execute the same command for each of them. Something like:

for *.conf
script.sh [conf-file]


so if there are 100 .conf-files in the folder, it should execute 'script.sh [conf-file] for each and every one of them (where [conf-file] of course is the name of the individual conf-files).


Top
 Profile WWW ICQ  
 PostPosted: Wed Nov 05, 2008 11:16 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
It IS a simple one! This is probably one of the loops I use most often in and out of scripts:

Code:
for i in $(ls *.conf); do
  script.sh "$i"
done


Another thing you could do is something like this with 'find':

Code:
find . -type f -name '*.conf' -exec script.sh {} \;


If your conf file names have spaces in them you might have to do something different...

-J


Top
 Profile YIM  
 PostPosted: Tue Nov 11, 2008 5:37 am   

Joined: Wed Nov 05, 2008 6:19 am
Posts: 7
That is indeed simple! Thanks! 8)


But, to make matters more difficult, I only need to pass on part of the filename to the script. All files have the format
[sometext].[WhatIWant].conf where [sometext] is the same for all files. And [WhatIWant] is the text I want to give to the script:

script.sh [WhatIWant]

Is there an easy way to accomplish this as well?


Top
 Profile WWW ICQ  
 PostPosted: Tue Nov 11, 2008 4:41 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Sure! If they're all in the same format, separated by dots, just do something like:

Code:
for i in $(ls *.conf|awk -F\. '{print $2}'); do
  script.sh "$i"
done


This still grabs everything with a .conf suffix, but we use awk, with the '.' as a field separator, to chop off whatever's in the beginning and the .conf at the end. Will that do it?

-J


Top
 Profile YIM  
 PostPosted: Wed Nov 12, 2008 2:15 am   

Joined: Wed Nov 05, 2008 6:19 am
Posts: 7
Perfect! 8)

That does the job. Thank you so much :)

I added a '&' so all instances of the script run at once now. :D

Greetings,
Evert


Top
 Profile WWW ICQ  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP