LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-07-2010, 12:02 PM   #1
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Rep: Reputation: 0
Need for loop to get dirs and run bash script


Greetings all!

This is my first post. I hope it's in the appropriate section. I have to format 4 years worth of awstats data "static" for a client and then move it to their new server.

I don't want to run the commend to do this 48 times. If possible I would like to use a bash script that uses the folders in a directory so the script knows which year-month to do this for me and which folder to place the output in.

The command I need to run is as follows;

Code:
perl /usr/share/awstats/awstats_buildstaticpages.pl -month=12 -year=2006 -config=domain.org -awstatsprog=/var/www/cgi-bin/awstats/awstats.pl -dir=/root/static_awstats/
In the example above the conversion will be for the 12th month in the year in 2006. The above would place the data in static_awstats. However, for the client to be able to browse this data on the new systen through awstats each year-month must be in it's own folder with the same name. For example I would like the above command to place the data in

Code:
/root/static_awstats/2006-12
Easy enough to do if I manually run the command for every year-month that I need. However I want to automate this using the script I hope to write.

I've already created the folders needed for the timeframe I need to use. So my question is.

Can I use the folders that I have created to run the command in a for loop to get all the pages done.

Example of my static_awstats dir;

Code:
/root/static_awstats
-bash-3.00# ls
2006-12  2007-04  2007-08  2007-12  2008-04  2008-08  2008-12  2009-04  2009-08  2009-12  2010-04  2010-08  2010-12
2007-01  2007-05  2007-09  2008-01  2008-05  2008-09  2009-01  2009-05  2009-09  2010-01  2010-05  2010-09  css
2007-02  2007-06  2007-10  2008-02  2008-06  2008-10  2009-02  2009-06  2009-10  2010-02  2010-06  2010-10  icon
2007-03  2007-07  2007-11  2008-03  2008-07  2008-11  2009-03  2009-07  2009-11  2010-03  2010-07  2010-11
So the script should do the following. Get the names of all the folders, run the command once for each folder it finds inserting the -month=$month -year=$year into the command for each time it's run.

I hope this question makes sense to everyone.

Thanks for any suggestions.
 
Old 07-07-2010, 12:14 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
Sure you just need to iterate numbers. This should do the whole of 2006 though 2010 inclusive:

Code:
for year in $(seq 2006 2010);do for month in $(seq -w 1 12);do perl /usr/share/awstats/awstats_buildstaticpages.pl -month=${month} -year=${year} -config=domain.org -awstatsprog=/var/www/cgi-bin/awstats/awstats.pl -dir=/root/static_awstats/${year}-${month};done;done

That's all one line which isn't nice to look at of course. If you want to look at it in a more readable form then paste it in to a text editor then replace the ; with line breaks

Last edited by arizonagroovejet; 07-07-2010 at 12:15 PM. Reason: typo
 
Old 07-07-2010, 04:46 PM   #3
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
arizonagroovejet,

This works perfectly thank you very much. I've learned allot just from that and managed to automate almost everything else I wanted from this example. I have one final question that I'm stuck on and it seems so simple.

I now need to symlink the base awstats.domain.org.html inside each folder to index.html to complete the conversion. From the base folder outside all the year-month folders I'm using the following;

Code:
for i in `ls`;do ln -s awstats.domain.org.html  index.html "$i";done
For some reason it's linking index.html to index.html and not to awstats.domain.org.html?

Code:
lrwxrwxrwx   1 root root     10 Jul  7 13:36 index.html -> index.html
I've tried several diffrent methods but can't get it to work. I could do these by hand I guess since most of the rest I didn't need to.

Thanks again for your help. That taught me allot and saved me a ton of time.

Bill
 
Old 07-07-2010, 05:03 PM   #4
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,088

Rep: Reputation: 196Reputation: 196
Assuming you want to end up with an index.html file in each of the ${year}-${month} directories then in the directory where those ${year}-${month} directories are located do

Code:
$ for i in *; do ln -s awstats.domain.org.html "${i}/index.html";done
That assumes there are only directories in the current working directory and each one contains a file called awstats.domain.org.html. You'll get errors if that's not the case when it attempts to perform the operation on things that aren't directories containing a file called awstats.domain.org.html
 
Old 07-07-2010, 05:21 PM   #5
bckr
LQ Newbie
 
Registered: Jul 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Your assumptions are correct. That worked perfectly. Thank you so much. I hope I can help someone here in the future.
 
Old 07-07-2010, 11:42 PM   #6
confidenceb
LQ Newbie
 
Registered: Jun 2008
Posts: 11

Rep: Reputation: 0
Quote:
Originally Posted by bckr View Post
Your assumptions are correct. That worked perfectly. Thank you so much. I hope I can help someone here in the future.
glad to see u have the problem solved
 
  


Reply

Tags
iterate


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Run SQL update statement in a for loop in bash testac Programming 8 06-11-2010 11:18 AM
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
Bash script to process every file in current dir and sub dirs BuckRogers01 Linux - Software 3 09-06-2005 08:32 AM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM


All times are GMT -5. The time now is 08:16 PM.

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