경과 시간을 계산하는 Bash 스크립트
명령 실행에 소요 된 시간을 계산하기 위해 bash에서 스크립트를 작성 중입니다. 다음을 고려하십시오.
STARTTIME=$(date +%s)
#command block that takes time to complete...
#........
ENDTIME=$(date +%s)
echo "It takes $($ENDTIME - $STARTTIME) seconds to complete this task..."
내 논리가 정확하다고 생각하지만 다음과 같은 인쇄물로 끝납니다.
"이 작업을 완료하는 데 몇 초가 걸립니다 ..."
내 문자열 평가에 문제가 있습니까?
나는 bash 변수가 형식화되지 않았다고 믿는다. 그럼에도 불구하고 bash에 "string to integer"메소드가 있다면 좋아할 것이다.
어느 $(())
또는 $[]
산술 연산의 결과를 계산하기 위해 작동한다. 당신 $()
은 단순히 문자열을 받아 명령으로 평가하는 것을 사용하고 있습니다. 약간 미묘한 차이입니다. 도움이 되었기를 바랍니다.
이 답변에 대한 주석에서 tink가 지적했듯이 $[]
더 이상 사용되지 않으며 $(())
선호되어야합니다.
내부 변수 "$ SECONDS"를 사용하는 것이 매우 깔끔합니다.
SECONDS=0 ; sleep 10 ; echo $SECONDS
ENDTIME
명령으로 의 번호를 실행하려고합니다 . 또한와 같은 오류가 표시되어야합니다 1370306857: command not found
. 대신 산술 확장을 사용하십시오 .
echo "It takes $(($ENDTIME - $STARTTIME)) seconds to complete this task..."
별도의 스크립트에 명령을 저장하고 commands.sh
time 명령을 사용할 수도 있습니다.
time commands.sh
time
적절한 형식 문자열과 함께 여기에서 Bash의 키워드를 사용할 수 있습니다.
TIMEFORMAT='It takes %R seconds to complete this task...'
time {
#command block that takes time to complete...
#........
}
다음은 참조가 말하는 내용입니다TIMEFORMAT
.
이 매개 변수의 값은
time
예약어로 시작하는 파이프 라인의 타이밍 정보를 표시하는 방법을 지정하는 형식 문자열로 사용 됩니다. '%
'문자는 시간 값 또는 기타 정보로 확장되는 이스케이프 시퀀스를 도입합니다. 이스케이프 시퀀스와 그 의미는 다음과 같습니다. 중괄호는 선택적 부분을 나타냅니다.%% A literal ‘%’. %[p][l]R The elapsed time in seconds. %[p][l]U The number of CPU seconds spent in user mode. %[p][l]S The number of CPU seconds spent in system mode. %P The CPU percentage, computed as (%U + %S) / %R.
선택적 p 는 소수점 뒤의 소수 자릿수 인 정밀도를 지정하는 숫자입니다. 값이 0이면 소수점이나 분수가 출력되지 않습니다. 소수점 이하 세 자리까지 지정할 수 있습니다. 3보다 큰 p 값은 3으로 변경됩니다. p 가 지정되지 않은 경우 값 3이 사용됩니다.
The optional
l
specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.If this variable is not set, Bash acts as if it had the value
$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.
Try the following code:
start=$(date +'%s') && sleep 5 && echo "It took $(($(date +'%s') - $start)) seconds"
try using time with the elapsed seconds option:
/usr/bin/time -f%e sleep 1
under bash.
or \time -f%e sleep 1
in interactive bash.
see the time man page:
Users of the bash shell need to use an explicit path in order to run the external time command and not the shell builtin variant. On system where time is installed in /usr/bin, the first example would become /usr/bin/time wc /etc/hosts
and
FORMATTING THE OUTPUT
...
% A literal '%'.
e Elapsed real (wall clock) time used by the process, in
seconds.
For larger numbers we may want to print in a more readable format. The example below does same as other but also prints in "human" format:
secs_to_human() {
if [[ -z ${1} || ${1} -lt 60 ]] ;then
min=0 ; secs="${1}"
else
time_mins=$(echo "scale=2; ${1}/60" | bc)
min=$(echo ${time_mins} | cut -d'.' -f1)
secs="0.$(echo ${time_mins} | cut -d'.' -f2)"
secs=$(echo ${secs}*60|bc|awk '{print int($1+0.5)}')
fi
echo "Time Elapsed : ${min} minutes and ${secs} seconds."
}
Simple testing:
secs_to_human "300"
secs_to_human "305"
secs_to_human "59"
secs_to_human "60"
secs_to_human "660"
secs_to_human "3000"
Output:
Time Elapsed : 5 minutes and 0 seconds.
Time Elapsed : 5 minutes and 5 seconds.
Time Elapsed : 0 minutes and 59 seconds.
Time Elapsed : 1 minutes and 0 seconds.
Time Elapsed : 11 minutes and 0 seconds.
Time Elapsed : 50 minutes and 0 seconds.
To use in a script as described in other posts (capture start point then call the function with the finish time:
start=$(date +%s)
# << performs some task here >>
secs_to_mins_and_secs "$(($(date +%s) - ${start}))"
start=$(date +%Y%m%d%H%M%S);
for x in {1..5};
do echo $x;
sleep 1; done;
end=$(date +%Y%m%d%H%M%S);
elapsed=$(($end-$start));
ftime=$(for((i=1;i<=$((${#end}-${#elapsed}));i++));
do echo -n "-";
done;
echo ${elapsed});
echo -e "Start : ${start}\nStop : ${end}\nElapsed: ${ftime}"
Start : 20171108005304
Stop : 20171108005310
Elapsed: -------------6
#!/bin/bash
time_elapsed(){
appstop=$1; appstart=$2
ss_strt=${appstart:12:2} ;ss_stop=${appstop:12:2}
mm_strt=${appstart:10:2} ;mm_stop=${appstop:10:2}
hh_strt=${appstart:8:2} ; hh_stop=${appstop:8:2}
dd_strt=${appstart:6:2} ; dd_stop=${appstop:6:2}
mh_strt=${appstart:4:2} ; mh_stop=${appstop:4:2}
yy_strt=${appstart:0:4} ; yy_stop=${appstop:0:4}
if [ "${ss_stop}" -lt "${ss_strt}" ]; then ss_stop=$((ss_stop+60)); mm_stop=$((mm_stop-1)); fi
if [ "${mm_stop}" -lt "0" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
if [ "${mm_stop}" -lt "${mm_strt}" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
if [ "${hh_stop}" -lt "0" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
if [ "${hh_stop}" -lt "${hh_strt}" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
if [ "${dd_stop}" -lt "0" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
if [ "${dd_stop}" -lt "${dd_strt}" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
if [ "${mh_stop}" -lt "0" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
if [ "${mh_stop}" -lt "${mh_strt}" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
ss_espd=$((10#${ss_stop}-10#${ss_strt})); if [ "${#ss_espd}" -le "1" ]; then ss_espd=$(for((i=1;i<=$((${#ss_stop}-${#ss_espd}));i++)); do echo -n "0"; done; echo ${ss_espd}); fi
mm_espd=$((10#${mm_stop}-10#${mm_strt})); if [ "${#mm_espd}" -le "1" ]; then mm_espd=$(for((i=1;i<=$((${#mm_stop}-${#mm_espd}));i++)); do echo -n "0"; done; echo ${mm_espd}); fi
hh_espd=$((10#${hh_stop}-10#${hh_strt})); if [ "${#hh_espd}" -le "1" ]; then hh_espd=$(for((i=1;i<=$((${#hh_stop}-${#hh_espd}));i++)); do echo -n "0"; done; echo ${hh_espd}); fi
dd_espd=$((10#${dd_stop}-10#${dd_strt})); if [ "${#dd_espd}" -le "1" ]; then dd_espd=$(for((i=1;i<=$((${#dd_stop}-${#dd_espd}));i++)); do echo -n "0"; done; echo ${dd_espd}); fi
mh_espd=$((10#${mh_stop}-10#${mh_strt})); if [ "${#mh_espd}" -le "1" ]; then mh_espd=$(for((i=1;i<=$((${#mh_stop}-${#mh_espd}));i++)); do echo -n "0"; done; echo ${mh_espd}); fi
yy_espd=$((10#${yy_stop}-10#${yy_strt})); if [ "${#yy_espd}" -le "1" ]; then yy_espd=$(for((i=1;i<=$((${#yy_stop}-${#yy_espd}));i++)); do echo -n "0"; done; echo ${yy_espd}); fi
echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}"
#return $(echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}")
}
mh_days(){
mh_stop=$1; yy_stop=$2; #also checks if it's leap year or not
case $mh_stop in
[1,3,5,7,8,10,12]) mh_stop=31
;;
2) (( !(yy_stop % 4) && (yy_stop % 100 || !(yy_stop % 400) ) )) && mh_stop=29 || mh_stop=28
;;
[4,6,9,11]) mh_stop=30
;;
esac
return ${mh_stop}
}
appstart=$(date +%Y%m%d%H%M%S); read -p "Wait some time, then press nay-key..." key; appstop=$(date +%Y%m%d%H%M%S); elapsed=$(time_elapsed $appstop $appstart); echo -e "Start...: ${appstart:0:4}-${appstart:4:2}-${appstart:6:2} ${appstart:8:2}:${appstart:10:2}:${appstart:12:2}\nStop....: ${appstop:0:4}-${appstop:4:2}-${appstop:6:2} ${appstop:8:2}:${appstop:10:2}:${appstop:12:2}\n$(printf '%0.1s' "="{1..30})\nElapsed.: ${elapsed}"
exit 0
-------------------------------------------- return
Wait some time, then press nay-key...
Start...: 2017-11-09 03:22:17
Stop....: 2017-11-09 03:22:18
==============================
Elapsed.: 0000-00-00 00:00:01
참고URL : https://stackoverflow.com/questions/16908084/bash-script-to-calculate-time-elapsed
'development' 카테고리의 다른 글
Java에서 문자열을 "문자"배열로 변환 (0) | 2020.08.18 |
---|---|
Intellij는 가져올 때 기호를 확인할 수 없습니다. (0) | 2020.08.18 |
Django에서 PostgreSQL 데이터베이스를 설정하는 방법 (0) | 2020.08.18 |
'ssh-keygen'은 내부 또는 외부 명령으로 인식되지 않습니다. (0) | 2020.08.18 |
DOM에서 중복 ID를 확인하는 JQuery (0) | 2020.08.18 |