Bash 嵌套循环,日期和数字的混合

发布于 2024-09-26 05:24:34 字数 316 浏览 5 评论 0原文

我正在尝试输出一系列具有不同日期和相关数字的命令。每小时例如。

我试图在循环中执行的输出是:

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

所以基本上上面的代码将生成一个输出,每个特定的一天执行 24 次,并具有唯一的 4 位 id。

我正在考虑有一个嵌套循环,因为批号需要​​是唯一的。

有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-10-03 05:24:34

目前尚不清楚为什么需要嵌套循环,或者是否需要迭代某个日期范围。但脚本将如下所示:

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

希望它有帮助!

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!

伪心 2024-10-03 05:24:34

我想做一个嵌套循环,因为我需要的输出如下:(

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文