Skip to content

Instantly share code, notes, and snippets.

@qileq
Last active July 29, 2022 06:10
Show Gist options
  • Select an option

  • Save qileq/d96c307aa17b29a29f7085cee73c67e2 to your computer and use it in GitHub Desktop.

Select an option

Save qileq/d96c307aa17b29a29f7085cee73c67e2 to your computer and use it in GitHub Desktop.
Shell common methods quick query
  • Generate date from start day to end day
    Smaple:
#!/bin/bash

cur="2022-03-01"
end="2022-03-31"
while [[ "$cur" < "$end" ]] 
do
  cur=`date -d "${cur} 1day" +"%Y-%m-%d"`
  echo "$cur"
done

output:

2022-03-01
2022-03-02
2022-03-03
2022-03-04
2022-03-05
2022-03-06
2022-03-07
2022-03-08
2022-03-09
2022-03-10
2022-03-11
2022-03-12
2022-03-13
2022-03-14
2022-03-15
2022-03-16
2022-03-17
2022-03-18
2022-03-19
2022-03-20
2022-03-21
2022-03-22
2022-03-23
2022-03-24
2022-03-25
2022-03-26
2022-03-27
2022-03-28
2022-03-29
2022-03-30
2022-03-31

According date sytle to modify %Y%m%d.

  • Generate hour from 00 to 23
    Smaple:
#! /bin/bash

cur=`date +"%Y-%m-%d"`
for i in {0..23}
do
  if [ $i -lt 10 ]; then
    cur_time="$cur 0$i"
  else
    cur_time="$cur $i"
  fi
  echo $cur_time
done

output:

2022-03-31 00
2022-03-31 01
2022-03-31 02
2022-03-31 03
2022-03-31 04
2022-03-31 05
2022-03-31 06
2022-03-31 07
2022-03-31 08
2022-03-31 09
2022-03-31 10
2022-03-31 11
2022-03-31 12
2022-03-31 13
2022-03-31 14
2022-03-31 15
2022-03-31 16
2022-03-31 17
2022-03-31 18
2022-03-31 19
2022-03-31 20
2022-03-31 21
2022-03-31 22
2022-03-31 23

  • Generate date and hour from start day to end day Sample:
#!/bin/bash

cur="2022-03-01"
end="2022-03-02"
while [[ ! "$cur" > "$end" ]]
do
  for i in {0..23}
  do
    if [ $i -lt 10 ]; then
      cur_time="$cur 0$i"
    else
      cur_time="$cur $i"
    fi
    echo $cur_time
  done
  cur=`date -d "${cur} 1day" +"%Y-%m-%d"`
done

output:

2022-03-01 00
2022-03-01 01
2022-03-01 02
2022-03-01 03
2022-03-01 04
2022-03-01 05
2022-03-01 06
2022-03-01 07
2022-03-01 08
2022-03-01 09
2022-03-01 10
2022-03-01 11
2022-03-01 12
2022-03-01 13
2022-03-01 14
2022-03-01 15
2022-03-01 16
2022-03-01 17
2022-03-01 18
2022-03-01 19
2022-03-01 20
2022-03-01 21
2022-03-01 22
2022-03-01 23
2022-03-02 00
2022-03-02 01
2022-03-02 02
2022-03-02 03
2022-03-02 04
2022-03-02 05
2022-03-02 06
2022-03-02 07
2022-03-02 08
2022-03-02 09
2022-03-02 10
2022-03-02 11
2022-03-02 12
2022-03-02 13
2022-03-02 14
2022-03-02 15
2022-03-02 16
2022-03-02 17
2022-03-02 18
2022-03-02 19
2022-03-02 20
2022-03-02 21
2022-03-02 22
2022-03-02 23

  • Generate last n days from someday
    Sample:
#!/bin/bash 

cur=`date +"%Y-%m-%d"`
end=30

for i in `seq 1 $end`  # or just {1..30}
do
  echo `date -d "$cur -${i}day" +"%Y-%m-%d"`
done

output:

2022-03-30
2022-03-29
2022-03-28
2022-03-27
2022-03-26
2022-03-25
2022-03-24
2022-03-23
2022-03-22
2022-03-21
2022-03-20
2022-03-19
2022-03-18
2022-03-17
2022-03-16
2022-03-15
2022-03-14
2022-03-13
2022-03-12
2022-03-11
2022-03-10
2022-03-09
2022-03-08
2022-03-07
2022-03-06
2022-03-05
2022-03-04
2022-03-03
2022-03-02
2022-03-01

@qileq
Copy link
Author

qileq commented Jun 3, 2022

:bowtie:

@qileq
Copy link
Author

qileq commented Jun 3, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment