views:

28

answers:

2

Hi,

I am trying to output a range of commands with different dates and numbers associated. for each hour eg.

Output im trying to do in a loop is:

shell.sh filename<number e.g. between 1-24> <date e.g. 20100928> <number e.g. between 1-24> <id>

So basically the the above will generate an output done 24 times for each particular day with a unique 4 digit id.

I was thinking of having a nested loop, as the batch number needs to be unique.

can anyone help?

A: 

It is not clear why you need a nested loop or whether you need to iterate over a range of dates or not. But the script will look like this:

#!/bin/bash

DAY_FROM=1 # This is a first (starting) day
DAY_TO=24  # This is the last day

DATE=$(date +%Y%m%d) # This is a date we are processing.

id=0 # This is a number for our unique ID generation.
     # It is being incremented for each day.
     # Since this variable is in global scope,
     # it will be unique no matter how many dates you process.
     # If you want unique ID be unique only for date scope,
     # reset it to 0 before processing each date.

# Let's go iterate over all days.
for (( i=$DAY_FROM; i <= $DAY_TO; ++i ))
do
    let ++id # Increment our unique ID number...
    # Print filename, date, number and unique ID.
    # %04d at the end means that we output an integer
    # with 4 digits padded with zeroes if needed.
    printf "%s %s %s %04d\n" "filename$i" "$DATE" "$i" "$id"
done

... and the output will be like this:

filename1 20101007 1 0001
filename2 20101007 2 0002
filename3 20101007 3 0003
....

Hope it helps!

Vlad Lazarenko
hey Vlad, thanks.I wanted to do a nested loop as output i need is like:filename1 20101007 01 0001filename2 20101007 02 0002filename3 20101007 03 0003filename4 20101007 04 0004filename5 20101007 05 0005filename6 20101007 06 0006......... ........ .. ....tofilename24 20101007 24 0024filename25 20101008 01 0025(as you can see a new set starts for a different date, this process keeps going for n date iteration)Thats why I was thinking nested loop :-S
Saleh
A: 

hey Vlad, thanks.

I wanted to do a nested loop as output i need is like:

filename1 20101007 01 0001
filename2 20101007 02 0002
filename3 20101007 03 0003
filename4 20101007 04 0004
filename5 20101007 05 0005
filename6 20101007 06 0006
......... ........ .. ....
to
filename24 20101007 24 0024

filename25 20101008 01 0025

(as you can see a new set starts for a different date, this process keeps going for n date iteration)

Thats why I was thinking nested loop :-S

Saleh Bari