DigiMoon 맘대로 닦고 조이고 기름치는 재미가 있는 DigiMoon만의 기억 저장소

Posted
Filed under 컴퓨터 탐구/리눅스
고객사에서 D-Day 날짜 계산할 수 있는 bash shell 소스를 구해 달라길래 구글링하다 찾은 괜춘한 소스

http://www.unix.com/tips-tutorials/31944-simple-date-time-calulation-bash.html


#!/bin/bash

date2stamp () {

    date --utc --date "$1" +%s

}

stamp2date (){

    date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"

}

dateDiff (){

    case $1 in

        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;

    esac

    dte1=$(date2stamp $1)
    dte2=$(date2stamp $2)
    diffSec=$((dte2-dte1))
    if ((diffSec < 0)); then abs=-1; else abs=1; fi
    echo $((diffSec/sec*abs))

}

## USAGE # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
## convert a date into a UNIX timestamp
#    stamp=$(date2stamp "2006-10-01 15:00")
#    echo $stamp
#
## from timestamp to date
#    stamp2date $stamp
#
## calculate the number of days between 2 dates
#    # -s in sec. | -m in min. | -h in hours  | -d in days (default)
#    dateDiff -s "2006-10-01" "2006-10-32"
#    dateDiff -m "2006-10-01" "2006-10-32"
#    dateDiff -h "2006-10-01" "2006-10-32"
#    dateDiff -d "2006-10-01" "2006-10-32"
#    dateDiff  "2006-10-01" "2006-10-32"
#
## number of seconds between two times
#    dateDiff -s "17:55" "23:15:07"
#    dateDiff -m "17:55" "23:15:07"
#    dateDiff -h "17:55" "23:15:07"
#
## number of minutes from now until the end of the year
#    dateDiff -m "now" "2006-12-31 24:00:00 CEST"
#
## Other standard goodies from GNU date not too well documented in the man pages
#    # assign a value to the variable dte for the examples below
#    dte="2006-10-01 06:55:55"
#    echo $dte
#
#    # add 2 days, one hour and 5 sec to any date
#    date --date "$dte  2 days 1 hour 5 sec"
#
#    # substract from any date
#    date --date "$dte 3 days 5 hours 10 sec ago"
#    date --date "$dte -3 days -5 hours -10 sec"
#
#    # or any mix of +/-. What will be the date in 3 months less 5 days
#    date --date "now +3 months -5 days"
#
#    # time conversions into ISO-8601 format (RFC-3339 internet recommended format)
#    date --date "sun oct 1 5:45:02PM" +%FT%T%z
#    date --iso-8601=seconds --date "sun oct 1 5:45:02PM"
#    date --iso-8601=minutes
#
#    # time conversions into RFC-822 format
#    date --rfc-822 --date "sun oct 1 5:45:02PM"


EXPIRE_DAY=`date -d "$(chage -l digimoon | grep 'Password expires' | awk -F: '{print $2}')"  +'%Y-%m-%d'`

TODAY=`date +'%Y-%m-%d'`

▼ /etc/login.defs 파일의 "PASS_MAX_DAYS" 옵션에 정의한 날짜에 의해 정해진 시스템 계정의 password expire 날짜가 다가오기까지 얼마나 남았는지 알려주기 위한 용도의 스크립트 예
(계정별 expire 날짜는 "chage -l <계정명>"으로 확인 가능)
# expire 날짜가 100일 이하 남은 경우 계정 경고


if [ "$(dateDiff -d "$TODAY" "$EXPIRE_DAY")" -le 100 ] ; then
echo "digimoon 계정이 expire 날짜로부터 $(dateDiff -d "$TODAY" "$EXPIRE_DAY")일 남았습니다!!!"
fi
Creative Commons License
2014/01/20 11:04 2014/01/20 11:04