Results 1 to 5 of 5

Thread: Bash script for creating a new month and year

  1. #1
    Join Date
    Apr 2010
    Posts
    87

    Bash script for creating a new month and year

    I just installed in stats that I generated with HTML files only with static files, it is impossible to keep a history of previous months. I would therefore like to modify my bash script stats generated every night at 01h for it moves the files in a directory every month. What kind of script must exist on the net and saw that my level is more than zero bash programing I do not know how.?! I think I had to do a test to see if it is the first of the month and if so, create a directory with the number of months - 1 and if one is in January, create a new directory with the number of years. If someone could give me a small base of the script that I'd change my order to arrive at and then if someone know a script already done everything would be super cool AC.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Bash script for creating a new month and year

    This method may bring you some trouble if your script is not running, for some reason, the first of the month. Once it comes on 2 or later, he found a dirty environment and will have undefined behavior. It is better to go on an approach that is fully controlled and leaves no uncertainty. In my case, for this kind of scripts, I have the following steps:
    Code:
    Get the day
     Get month
     Recover year
     # All this can be done by cutting the output of 'date' of a sudden cut or awk.
    
     If the directory does not exist the year
        Then Create the directory of the Year
     Endif
    
     Go to the directory of the Year
    
     If the directory does not exist in the month
        Then create the directory of the month
     Endif
    
     Go to the directory of the month
    
     If the directory does not exist the day
        Then create the directory of the Day
     Endif
    
     Go to the directory of the Day
    
     Copy the desired files
    All this is done very easily in bash and a perfectly deterministic behavior. Good luck for coding.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Bash script for creating a new month and year

    You can retrieve the date (or any part) as follows:
    Code:
    date + '% 02Y-% 02m'
    in the example above that you return: 10-08
    Working on this basis:
    Code:
    # Recovery of the current directory (month + year)
     Current = $ (date + '% 02Y-02m')
     rep_courant mydir = $ current
     # If the directory does not exist we create
     if [!  -D $ rep_current]
     Then
         mkdir $ rep_current
     fi
    
     # Save in $ rep_current

  4. #4
    Join Date
    Apr 2010
    Posts
    87

    Re: Bash script for creating a new month and year

    In terms of stats I did that :
    Code:
     # / Bin / sh
     # List of fields
     field [0] = site
     field [1] = site1
     field [2] = site2
     field [3] = site3
     field [4] = site4
     field [5] = site5
    
     # Calculating stats
     for i in 0 1 2 3 4 5
     do
             rep_current_year = / home / stats / $ (domain [i]) / `date +% Y`
             rep_current_month = / home / stats / $ (domain [i]) / `date +% Y` / `date +% m`
             if [!  -D $ rep_current_year]
                     Then
                             mkdir $ rep_current_year
             fi
             if [!  -D $ rep_current_month]
                     Then
                             mkdir $ rep_current_month
             fi
    
             / Usr / share / doc / awstats / examples / awstats_buildstaticpages.pl-update-config = $ (area [i])-lang = en-dir = $ rep_current -
     awstatsprog = / usr / lib / cgi-bin / awstats.pl
     therefore
    What do you think?

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Bash script for creating a new month and year

    I am sure that the following code would be helpful for you :
    Code:
     # / Bin / sh
                                                                                    
                                                                                    
     domain [0] = site
     field [1] = site1
     field [2] = site2
     field [3] = site3
     field [4] = site4
     field [5] = site5
                                                                                    
     # Print the whole picture:
     echo $ (area[*])
                                                                                    
     # Print the array size
     echo $ (# domain[*])
                                                                                    
     # So
                                                                                    
     i = 0
     while ((i <$ (# domain[*])))
     do
        contained echo $ (area [$ i])
             ((I = $ i +1))
     therefore

Similar Threads

  1. calculation of date, month and year using excel
    By Edgar-Arular in forum Windows Software
    Replies: 4
    Last Post: 06-04-2011, 04:26 PM
  2. How to create CGI script in bash?
    By Mulan in forum Software Development
    Replies: 5
    Last Post: 01-09-2010, 08:19 PM
  3. Create bash script for ftp
    By teenQ in forum Operating Systems
    Replies: 4
    Last Post: 04-03-2010, 07:37 PM
  4. Creating array with Bash shell
    By Carnie in forum Operating Systems
    Replies: 5
    Last Post: 19-02-2010, 07:54 PM
  5. Bash script if then else help
    By Unix'EM in forum Software Development
    Replies: 3
    Last Post: 18-08-2009, 06:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,021,656.73953 seconds with 17 queries