Bash shell 하루 / 한시간씩 증가

최지영·2024년 8월 27일
0

🎈 Bash Shell에서 파라미터로 받은 Date 날짜를 증가 시키기

가끔씩 한동안 사용하지 않다가 다시 사용하려고 하는 경우 잊어먹는 경우가 많아 여기에 간단하게 정리~

  • 30일전부터 파라미터로 받은 날짜까지 하루씩 증가 시키는 스크립트
#!/bin/bash

end_date=$1


start_date=$(date -d "$end_date -30 days" +"%Y%m%d")

current_date=$start_date

echo $current_date

while [[ "$current_date" -le "$end_date" ]]; do
	echo "$current_date"
	current_date=$(date -d "$current_date +1 day" +"%Y%m%d")
done
  • 파라미터로 받은 날짜에서 24시간 전부터 파라미터 날짜까지 한시간씩 증가시키는 스크립트
#!/bin/bash

# 입력받은 yyyymmddhh 날짜 및 시간
end_datetime=$1

# 입력된 날짜 및 시간을 epoch 시간(초 단위)으로 변환
end_epoch=$(date -d "${end_datetime:0:8} ${end_datetime:8:2}" +"%s")

before_hour=24
# 24시간 전의 epoch 시간 계산
start_epoch=$(($end_epoch - $before_hour*3600))

# 현재 epoch 시간이 end_epoch보다 작거나 같을 때까지 실행
current_epoch=$start_epoch
while [ "$current_epoch" -le "$end_epoch" ]; do
    # 현재 시간을 yyyymmddhh 형식으로 출력
    date -d "@$current_epoch" +"%Y%m%d%H"
    # 한 시간씩 증가
    current_epoch=$(($current_epoch + 3600))
done

~                       

0개의 댓글