#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    2
    Rep Power
    0

    String functions with BASH scripting


    I'm having trouble getting into the scripting for BASH, this typeless variables are really confusing me sometimes.

    For example, i am trying to generate a string address that i can wget. I use the following code:

    for i in `seq 1997 2003`; do
    for j in `seq 1 12`; do
    for k in `seq 1 31`; do
    wget http://www.somedomain.com/$i$j$k.txt
    done
    done
    done

    Unfortunately, the web server i am trying to connect to has it's files arranged in this format:
    yyyymmdd - which means that mm or dd may contain a leading zero.
    I was wondering how could you check if $j and $k are only one character long and append a zero to it?

    Thanks for any help in advanced
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    j=`printf '%.02d' $j`

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    2
    Rep Power
    0
    Thanks a lot, it works
    However, there is a little problem... the file im trying to wget is a series, and it starts on 1997, 9th of Augest.

    Im not sure if i understand the looping commands at the beginning, but when i do this:

    for j in `seq 8 12`; do
    for k in `seq 9 31`; do

    // blah bah

    done
    done

    when it has finished trying to get 19970809, it will then go to 19970010

    what am i doing wrong?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    this sounds quite complicated to do with loops. and i donīt have this "seq" command installed on any of my boxes, so i canīt look up the man pages either. does it also work eg. like this?

    for i in `seq 01/01/2001 12/31/2002`

    if not, sorry, i donīt have an out-of-the-box solution for you. you need to make up some logic like:
    - first get the remaining days of the first month
    - then get the remaining months of the first year
    - then start over with 01/01 of the next year
    ....

IMN logo majestic logo threadwatch logo seochat tools logo