본문 바로가기
언어/shell script

Shell Script time estimate 시간 경과 계산

by darkdevilness 2022. 9. 2.
728x90

Bash has a handy SECONDS builtin variable that tracks the number of seconds that have passed since the shell was started. This variable retains its properties when assigned to, and the value returned after the assignment is the number of seconds since the assignment plus the assigned value.

Thus, you can just set SECONDS to 0 before starting the timed event, simply read SECONDS after the event, and do the time arithmetic before displaying.

#!/usr/bin/env bash

SECONDS=0
# do some work
duration=$SECONDS
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."

TZ=UTC0 printf '%(%H:%M:%S)T\n' "$duration"
echo $TZ

elapsedseconds 를  시/분/초 로 변경하려면 아래와 같이  UTC0 함수를 사용하면 된다.

$ hours=30;mins=12;secs=24
$ elapsedseconds=$(( ((($hours*60)+$mins)*60)+$secs ))
$ TZ=UTC0 printf '%(%H:%M:%S)T\n' "$elapsedseconds"
06:12:24
728x90

'언어 > shell script' 카테고리의 다른 글

Shell Script String 숫자로 변경  (0) 2022.09.01
linux shell 실행인자  (0) 2015.12.02
변수 관련 내요  (2) 2012.10.22