LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need for loop to get dirs and run bash script (https://www.linuxquestions.org/questions/linux-newbie-8/need-for-loop-to-get-dirs-and-run-bash-script-818525/)

bckr 07-07-2010 11:02 AM

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.

arizonagroovejet 07-07-2010 11:14 AM

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

bckr 07-07-2010 03:46 PM

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

arizonagroovejet 07-07-2010 04:03 PM

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

bckr 07-07-2010 04:21 PM

Your assumptions are correct. That worked perfectly. Thank you so much. I hope I can help someone here in the future.

confidenceb 07-07-2010 10:42 PM

Quote:

Originally Posted by bckr (Post 4026707)
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


All times are GMT -5. The time now is 05:10 PM.