简体   繁体   English

如何在Shell脚本中生成顺序日期/时间?

[英]How to generate sequential dates/times in a shell script?

I would like to download a bunch of data sets from 1981 to 2000 (20 years). 我想下载1981年至2000年(20年)的大量数据集。 Those are in every 10 minutes. 这些是每10分钟一次。 I was trying to write a script which will call all times and download the data. 我试图编写一个脚本,该脚本将始终调用并下载数据。 But I am unable to complete it. 但是我无法完成它。 I can't check the leap years and days in each month. 我无法检查每个月的the年和日子。 My script is: 我的脚本是:

#!/bin/sh
for yr in {1981..2000};do
  for mm in 01 02 03 04 05 06 07 08 09 10 11 12;do
    for dd in {1..31};do
      if [[ $dd -le 9 ]];then nn=0$dd;else nn=$dd;fi
      for tt in 00 10 20 30 40 50; do
        echo wget www.xyz.com/$yy/$mm/$nn/$tt.txt
      done;
     done;
    done;
  done

How can I fix the problems of leap years, and days in the month generally? 我该如何解决years年,以及通常每个月中的几天的问题?

You seem to have left out the hours. 您似乎没有时间了。

Assuming you have GNU date , you can deal with it by using the date calculations. 假设您有GNU date ,则可以使用日期计算来处理它。 Do you have to worry about switches between winter and summer (standard and daylight saving) time? 您是否需要担心冬季和夏季(标准时间和夏令时)之间的切换? If so, there'll be some entertainment to be had with gaps of an hour in the spring and a period in the fall when the raw date/time values repeat. 如果是这样,那么在春季和春季之间会有一些娱乐性,原始日期/时间值会重复一些间隔。

$ /opt/gnu/bin/date -d '1981-01-01 00:00:00' +'%s %Y-%m-%d %H:%M:%S'
347184000 1981-01-01 00:00:00
$ /opt/gnu/bin/date -d '2000-12-31 23:50:00' +'%s %Y-%m-%d %H:%M:%S'
978335400 2000-12-31 23:50:00
$

That gives you start and end times in Unix timestamp notation (and in the US/Pacific time zone — adjust to suit your needs). 这样就可以使用Unix时间戳记标记(以及美国/太平洋时区-根据您的需要进行调整)来开始和结束时间。 You could then use a loop such as: 然后,您可以使用如下循环:

now=347184000
end=978335400
while [ "$now" -le "$end" ]
do
    url=$(date -d "@$now" +'www.example.com/%y/%m/%d/%H/%M.txt')
    echo wget "$url"
    now=$(($now + 600))
done

There are multiple ways of writing that. 有多种写法。 I've assumed that there's a directory of hourly files, and within that the 10-minute files, but you can tweak the format to suit your requirements. 我假设有一个每小时文件目录,并且其中有10分钟的文件,但是您可以调整格式以适合您的要求。 The use of @ in the -d is crucial. -d使用@至关重要。

You might prefer to use a scripting language such as Perl or Python instead of repeatedly invoking date as shown. 您可能更喜欢使用诸如Perl或Python之类的脚本语言,而不是如图所示反复调用date

Note that you have a vast number of files to collect. 请注意,您要收集大量文件。 With about 31 million seconds per year, and 600 seconds per 10 minute interval, you're looking at over 50,000 files per year for 20 years, or 1 million files in total. 每年大约有3,100万秒,每10分钟间隔600秒,那么您在20年中每年要查看50,000多个文件,或总计100万个文件。 The target (victim) web site might not be happy with you running that flat out. 目标(受害者)网站可能对您运行该平台不满意。 You'd probably need to pace the retrieval operations — check their terms and conditions. 您可能需要调整检索操作的速度-检查其条款和条件。

This is how it can be (please note that this leap year calculation is good until 2100 only): 可以这样(请注意,此leap年计算仅在2100之前有效):

#!/bin/sh
for yr in {1981..2000};do
  for mm in 1 2 3 4 5 6 7 8 9 10 11 12;do
    for dd in {1..31};do
     if [[ $dd -eq 31 ]] && ( [[ $mm -eq 4 ]] || [[ $mm -eq 6 ]] || [[ $mm -eq 9 ]] || [[ $mm -eq 11 ]] )
     then
         continue
     elif ( [[ $dd -gt 28 ]] && [[ $mm -eq 2 ]] && [[ $(( $yr % 4 )) -ne 0 ]] ) || ([[ $dd -gt 29 ]] && [[ $mm -eq 2 ]] )
     then
         continue
     fi

      if [[ $mm -le 9 ]];then mon=0$mm;else mon=$mm;fi

      if [[ $dd -le 9 ]];then nn=0$dd;else nn=$dd;fi
      for tt in 00 10 20 30 40 50; do
        echo wget www.xyz.com/$yy/$mon/$nn/$tt.txt
      done;
     done;
    done;
 done

I would use something to figure out the leap years etc for me ie date. 我会用一些东西来计算出the年等日期。 The following might give a hint on how to do this. 以下内容可能提示如何执行此操作。

They way you're using wget means it's going to create a bunch of files with 他们使用wget的方式意味着它将使用以下方式创建一堆文件

"10.txt.1"
"10.txt.2"
"10.txt.3"
"10.txt.4"
"10.txt.5"

This might be fine but if you want to put these in a directory on their own or to name the file as something else 这样做可能很好,但是如果您想将它们自己放置在目录中或将文件命名为其他名称

#!/bin/bash
#Jan 01 1980
COUNTER=347155200 

while [ $COUNTER -lt 978263999 ]; do
  year=`date -r $COUNTER +"%y"`;
  month=`date -r $COUNTER +"%m"`;
  day=`date -r $COUNTER +"%d"`;
  hour=`date -r $COUNTER +"%H"`;
  min=`date -r $COUNTER +"%M"`;
  let COUNTER=COUNTER+600
  url="www.xyz.com/$year/$month/$day/$hour/$min.txt";
  dir="$year/$month/$day/$hour";
  file="$year/$month/$day/$hour/$min.txt"
  mkdir -p $dir;
  wget "$url" $file;
  #Post process files here...
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM