UNIX SHELL PROGRAMING 날짜/시간 계산 하기, 수행시간

2013. 11. 15. 13:3099. 정리전 - IT/29. IT 잡동지식

UNIX 용

 

전일 명일 구하기

 

#!/bin/sh

#+--------------------------+
#| CALC DATE                |
#|       2013.11.19 by Donz |
#+--------------------------+

#+--------------------------+
#| INPUT DATE               |
#+--------------------------+
function indate() {
    DATE=$1
    YEAR=`expr ${DATE:0:4} + 0`
    MONTH=`expr ${DATE:4:2} + 0`
    DAY=`expr ${DATE:6:2} + 0`

    # LASTDAY OF MONTH
    LASTDAY=`cal $MONTH $YEAR | tr -s " " "\n" | tail -1`
}

#+--------------------------+
#| MAKE 0+DAY               |
#+--------------------------+
function make00() {
      if [[ $DAY -lt 10 ]]
    then DAY=0$DAY
      fi

      if [[ $MONTH -lt 10 ]]
    then MONTH=0$MONTH
      fi
}

#+--------------------------+
#| YESTERDAY                |
#+--------------------------+
function yesterday() {
    indate $1

      if [[ "$MONTH" -eq "01" ]] && [[ "$DAY" -eq "01" ]]
    then MONTH=12
         (( YEAR=$YEAR - 1 ))
         DAY=$LASTDAY
    elif [[ "$MONTH" -ne 01 ]] && [[ "DAY" -eq 01 ]]
    then (( MONTH=$MONTH - 1 ))
         DAY=$LASTDAY
    else (( DAY=$DAY - 1 ))
      fi

    make00
}

#+--------------------------+
#| TOMORROW                 |
#+--------------------------+
function tomorrow() {
    indate $1

      if [[ "$MONTH" -eq "12" ]] && [[ "$DAY" -eq "$LASTDAY" ]]
    then MONTH=1
         (( YEAR=$YEAR + 1 ))
         DAY=1
    elif [[ "$MONTH" -ne 12 ]] && [[ "DAY" -eq "$LASTDAY" ]]
    then (( MONTH=$MONTH + 1 ))
         DAY=1
    else (( DAY=$DAY + 1 ))
      fi

    make00
}

#+--------------------------+
#| CHECK VALIDATION OF DATE |
#+--------------------------+
  if [[ "$1" -eq "" ]]
then echo Parameter does not exist!
     exit 0
  fi

  indate $1

  if [[ "$DAY" -gt "LASTDAY" ]]
then echo Invalid input date $1
     exit 0
  fi

#+--------------------------+
#| RUN BATCH                |
#+--------------------------+

yesterday $1
echo YESTERDAY : [$YEAR] [$MONTH] [$DAY]

indate $1
echo T O D A Y : [$YEAR] [$MONTH] [$DAY]

tomorrow $1
echo TOMORROW  : [$YEAR] [$MONTH] [$DAY]

 

 

 

 

 

 

LINUX 용

 

 

월의 마지막 날 구하기

 

예제) 2014년 2월의 마지막날 구하기

 

> cal 2 2014 | tr -s " " "\n" | tail -1

 

설명

tr -s A B : A를  B로 치환

tail -1 : 출력된 값의 마지막 줄만 출력

 

 

 

FUNCTION 3가지 사용법

1. echo strings

lockdir="somedir"
testlock(){
    retval=""
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval="true"
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval="false"
    fi
    echo "$retval"
}

retval=$( testlock )
if [ "$retval" == "true" ]
then
     echo "directory not created"
else
     echo "directory already created"
fi 

 

 

2. return exit status

lockdir="somedir"
testlock(){
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval=0
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval=1
    fi
    return "$retval"
}

testlock
retval=$?
if [ "$retval" == 0 ]
then
     echo "directory not created"
else
     echo "directory already created"
fi 

 

 

3. share variable

lockdir="somedir"
retval=-1
testlock(){
    if mkdir "$lockdir"
    then # directory did not exist, but was created successfully
         echo >&2 "successfully acquired lock: $lockdir"
         retval=0
    else
         echo >&2 "cannot acquire lock, giving up on $lockdir"
         retval=1
    fi
}

testlock
if [ "$retval" == 0 ]
then
     echo "directory not created"
else
     echo "directory already created"
fi  

 

 

 

# date

Thu Nov 19 22:56:09 KST 2009
# date -d -1months
Mon Oct 19 22:56:23 KST 2009
# date -d +1years
Fri Nov 19 22:56:38 KST 2010
# date -d -1days
Wed Nov 18 22:56:57 KST 2009
# date -d -1minutes
Thu Nov 19 22:56:15 KST 2009
# date
Thu Nov 19 22:57:19 KST 2009
# date -d -1hours
Thu Nov 19 21:57:33 KST 2009

date +%C%y%m%d
20090902(금일)
date +%C%y%m%d -d -1days
20090901(어제)

 

HP-UX

root # date
2010년9월26일 (일), 오후 3시52분58초

root # TZ=KST+15 date
2010년9월25일 (토), 오후 3시53분0초

root # TZ=KST+14 date
2010년9월25일 (토), 오후 4시53분9초

root # TZ=KST+13 date
2010년9월25일 (토), 오후 5시53분11초

root # TZ=KST+12 date
2010년9월25일 (토), 오후 6시53분12초

root # TZ=KST+11 date
2010년9월25일 (토), 오후 7시53분14초

 

 

 

 

예제 출처 : http://starlandm.blogspot.kr/2009/11/unix-shell-%EB%82%A0%EC%A7%9C%EC%8B%9C%EA%B0%84-%EA%B3%84%EC%82%B0-%ED%95%98%EA%B8%B0-date-d-1days.html

#!/bin/ksh

## SET Variable
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
#YEAR=2011
#MONTH=1
#DAY=1

MONTH_NAME=""
SETI=8 # modify you want n times
TEMPI=0 # leave set 1

echo "TODAY IS $YEAR$MONTH $DAY"

## calculate last month's last day #input) $MONTH $1
GET_PMONTH_LDAY()
{
  MONTH=`expr $1 - 1`
  DAY=`cal $MONTH $YEAR | tail +3 | wc -w`
}

## calculate last month #input) $MONTH $YEAR $1 $2
GET_PMONTH()
{
   if [ $1 -eq 1 ] # case - Jan 1st
   then
     YEAR=`expr $2 - 1`
     MONTH=12
   else
     MONTH=`expr $1 - 1`
   fi
}

## calculate yesterday  #input) $DAY $MONTH $YEAR
GET_PDAY()
{
  if [ $2 -eq 1 -a $1 -eq 1 ] # case - Jan 1st
   then
     YEAR=`expr $3 - 1`
     DAY=31
     MONTH=12
  elif [ $1 -eq 1 ] # case - every month 1st day
   then
     GET_PMONTH_LDAY $MONTH
  else # case - Normal day
     DAY=`expr $1 - 1`
  fi
}

#### 월숫자를 Jan 타입으로 변환하기

## Set MONTH_NAME   11->Nov #input) $MONTH $1
GET_MONTH_NAME()
{
  case $1 in
1 ) MONTH_NAME="Jan"
;;
2 ) MONTH_NAME="Feb"
;;
3 ) MONTH_NAME="Mar"
;;
4 ) MONTH_NAME="Apr"
;;
5 ) MONTH_NAME="May"
;;
6 ) MONTH_NAME="Jun"
;;
7 ) MONTH_NAME="Jul"
;;
8 ) MONTH_NAME="Aug"
;;
9 ) MONTH_NAME="Sep"
;;
10 ) MONTH_NAME="Oct"
;;
11 ) MONTH_NAME="Nov"
;;
12 ) MONTH_NAME="Dec"
;;
* ) ;;
esac
}

 

 

프로그램 수행시간

start_time=`date +%s` 

start_time_string=`date`

###

# Run command

###

 

end_time=`date +%s` 

end_time_string=`date`

elapsed_time=`echo "$end_time - $start_time" | bc`

htime=`echo "$elapsed_time/3600" | bc` 

mtime=`echo "($elapsed_time/60) - ($htime * 60)" | bc` 

stime=`echo "$elapsed_time - (($elapsed_time/60) * 60)" | bc` 

 

echo "=============================================="

echo "Start time : $start_time_string"

echo "End time   : $end_time_string"

echo "Total time : ${htime} H ${mtime} M ${stime} S"

echo "=============================================="