몇 가지 가능성 :
감산 시간 date
로부터 얻어지는 :
#!/bin/bash
startdate=$(date +%s)
sleep 130
enddate=$(date +%s)
timetaken=$((enddate-startdate))
printf 'Total downtime was %d minutes and %d seconds\n' "$((timetaken/60))" "$((timetaken%60))"
외부 프로세스 date
않고 동일한 (배시 4.2 이후)
#!/bin/bash
printf -v startdate '%(%s)T' -1
sleep 130
printf -v enddate '%(%s)T' -1
timetaken=$((enddate-startdate))
printf 'Total downtime was %d minutes and %d seconds\n' "$((timetaken/60))" "$((timetaken%60))"
시간 빼기 및 분 및 초 계산은 arithmetic expansion을 사용하여 수행됩니다.
#!/bin/bash
SECONDS=0 # reset the SECONDS variable
sleep 130
timetaken=$SECONDS
printf 'Total downtime was %d minutes and %d seconds\n' "$((timetaken/60))" "$((timetaken%60))"
정수 값 특별한 변수 SECONDS
로 설정 된 후 각 초를 증가 : Bash's SECONDS
variable가 (아마 당신을위한 최고의) 사용
. 여기에 적절한 TIMEFORMAT
(와 강타의 time
키워드를 사용
, 우리는 MM은 분, SS 초과 경과 시간을 쓸 수 없습니다, 그것은 MmSs
의 형태, 즉에 표시됩니다 , 총 중단 시간은 2 분 10 초입니다. 이미 물질을 많이 포함 linked answer 것을
#!/bin/bash
TIMEFORMAT='Total downtime was %0lR'
time {
# do your stuff in this block
sleep 130
}
참고.
이들은 모두 한 줄로 나왔습니다. 죄송 해요. 나는 그것이이 포럼에서 어떻게 적절하게 코드화 될 수 있는지를 몰랐다. 왜냐하면 난 새거야. – kkrrss
줄의 4 개의 공백으로 코드를 들여 써야합니다. 또한 http://stackoverflow.com/help/mcve에서보십시오 –
http://stackoverflow.com/questions/8903239/how-to-calculate-time-difference-in-bash-script 이것을 보셨습니까? –