Shell Scripting提取日期

时间:2018-05-07 18:11:55

标签: shell logic

在shell脚本中需要一个逻辑,我给出了开始和结束日期, 说 startDate=20140101& endDate=20160130

我应该能够在第一个循环中提取startDate_new=20140122endDate_new=20140221

在下一个循环中需要startDate_new=20140222endDate_new=20140321等等,直到startDate_new=20151221& endDate_new=20160122并退出。您能否提供可用于实现此目的的任何shell脚本逻辑。

test.sh有,

startdate=2013-03-01
enddate=2013-04-30

curr="$startdate"
while true; do
    echo "$curr"
    [ "$curr" \< "$enddate" ] || break
    curr=$( date +%Y-%m-%d --date "$curr +1 day" )
done

上面的代码打印,

2013-03-01
2013-03-02
2013-03-03
2013-03-04
2013-03-05
2013-03-06
2013-03-07
2013-03-08
.
.
.
.
2013-04-30

你能帮我解决一下上面提到的范围吗?

想出了一个新的逻辑,

startdate=2013-03-21
enddate=2014-05-30

curr="$startdate"
while true; do
    [ "$curr" \< "$enddate" ] || { echo "$curr"; break; }
    echo "$curr"
    curr=$( date +%Y-%m-%d --date "$curr +1 month" )
    end=$( date +%Y-%m-%d --date "$curr +1 month +1day" )
done

以上逻辑给出了

2013-03-21
2013-04-21
2013-05-21
2013-06-21
2013-07-21
2013-08-21
2013-09-21
2013-10-21
2013-11-21
2013-12-21
2014-01-21
2014-02-21
2014-03-21
2014-04-21
2014-05-21
2014-06-21

你能帮忙解决如何在每月22日之前检索结束日期吗?

1 个答案:

答案 0 :(得分:0)

创建一个功能:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <Windows.h>

#define MAX 15 //word max size

struct slowa
{
    char pl_word[MAX];
    char eng_word[MAX];
};

void menu(void);
int count_lines(FILE*);
void print_array1 (const int[],int); //print 1-dimension array
void swap (int*,int*);

int main ()
{
    srand( time( NULL ) );
    int i,k,j;
    FILE* fp;
    fp = fopen ("tekst", "r");
    if (!fp)
    {
        printf ("zjebales"); //"you fucked up"
        return -1;
    }
    const int lines = count_lines(fp);
    int tab[lines]= {};
    int* temp = (int*)malloc(lines*sizeof(int));
    for (j=0; j<lines; j++)
    {
        temp[j]=j+1;
    }
    j=lines;
    for (i=0; i<lines; i++)
    {
        k=rand()%j--;
        tab[i]=temp[k]-1;
        swap(temp+k,temp+j);
    }
    free(temp);
    struct slowa arr[lines];
    rewind (fp);
    for (i=0; i<lines; i++)
    {
        fscanf(fp, "%s %s", arr[tab[i]].pl_word, arr[tab[i]].eng_word);
    }
    fclose(fp);
    char text[MAX];
    k=0;
    system("cls");
    menu();
    scanf("%d",&j);
    system("cls");
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF);

    switch (j)
    {
    case 1:
        for (i=0; i<lines; i++)
        {
            printf ("Podaj tlumaczenie:%s\n", arr[i].eng_word); //"Write translation"

            //fgets (text, MAX, stdin);
            scanf ("%s",text);

            if (strcmp(arr[i].pl_word, text) == 0)
            {
                puts ("DOBRZE!"); //"Correct!"
                k++;
            }
            else puts ("ZLE!"); //"Wrong"
            Sleep(500);
            system("cls");
        }

        break;
    case 2:
        for (i=0; i<lines; i++)
        {
            printf ("Podaj tlumaczenie:%s\n", arr[i].pl_word); //"Write translation"
            scanf ("%s",text);

            if (strcmp(arr[i].eng_word, text) == 0)
            {
                puts ("DOBRZE!"); //Correct!
                k++;
            }
            else puts ("ZLE!"); //Wrong!
            Sleep(500);
            system("cls");
        }
        break;

    default:
        puts ("miales podac 1 lub 2 dzbanie"); //"You should put 1 or 2"
        getchar();
        return -1;
    }
    printf ("Odpowiedziales dobrze na %d/%d pytan. ", k, lines); //"You answered correctly k/lines words"
    if (k==lines) puts("No dobra cos tam umiesz");
    else if(k>(lines/2)&&k<lines) puts ("Mogloby byc lepiej");
    else puts ("Wracaj do nauki debilu");
    while ((ch = getchar()) != '\n' && ch != EOF);

    getchar();
    return 0;
}

像这样运行:

# Usage: drange startdate endate [ time_increment ]
drange () 
{ 
    curr="$( date +%Y-%m-%d --date $1)"
    enddate="$( date +%Y-%m-%d --date $2)"
    shift 2 
    inc="${*:-+1 day}"
    until [ "$curr" \> "$enddate" ]; do
        echo $curr
        curr=$( date +%Y-%m-%d --date "$curr $inc" )
    done
}