0

さまざまな日付と番号が関連付けられた一連のコマンドを出力しようとしています。毎時間例えば。

ループで実行しようとしている出力は次のとおりです。

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

したがって、基本的に上記は、一意の 4 桁の ID を持つ特定の日ごとに 24 回実行される出力を生成します。

バッチ番号は一意である必要があるため、ネストされたループを持つことを考えていました。

誰でも助けることができますか?

4

2 に答える 2

0

必要な出力が次のようなものであるため、ネストされたループを実行したかったのです。

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

(別の日付で新しいセットが開始されていることがわかるように、このプロセスは n 日の繰り返しで続行されます)

それが、ネストされたループを考えていた理由です:-S

于 2010-10-11T00:44:03.687 に答える
0

ネストされたループが必要な理由や、日付の範囲を反復する必要があるかどうかは明らかではありません。ただし、スクリプトは次のようになります。

#!/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

...出力は次のようになります。

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

それが役に立てば幸い!

于 2010-10-07T21:24:17.953 に答える