Development/Shell script
[Shell Script] 현재 시간 정보 가져오기
Best Junior
2020. 1. 6. 10:28
Linux의 date 커맨드 결과값을 cut으로 잘라 일부를 변수에 넣는 형태 예시
[ date 커맨드 출력값 예시 ]
$ date
2023. 01. 06. (월) 08:44:59 KST
#!/bin/bash
year=`date |cut -d '.' -f1`
month=`date |cut -d '.' -f2 |cut -d ' ' -f2`
day=`date |cut -d '.' -f3 |cut -d ' ' -f2`
day_of_the_week=`date |cut -d '(' -f2 |cut -d ')' -f1`
hour=`date |cut -d ' ' -f5 |cut -d ':' -f1`
minute=`date |cut -d ' ' -f5 |cut -d ':' -f2`
second=`date |cut -d ' ' -f5 |cut -d ':' -f3`
echo $year-$month-$day $day_of_the_week $hour:$minute:$second
# 출력값 예시 : 2023-01-06 월 10:27:31