2013. 11. 15. 13:30ㆍ99. 정리전 - 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
HP-UX 용
프로그램 수행시간
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 "=============================================="