Bash:以天、时、分、秒显示秒数
Bash: Display Seconds in Days, Hours, Minutes, Seconds
我有一个函数,灵感来自 here 的 hms
,但我希望扩展它以包括处理和显示天数。
我已经开始编辑脚本,但很快意识到我无法处理将小时 运行 变成几天的逻辑,反之亦然...
这是我目前的情况:
#!/bin/bash
rendertimer(){
# convert seconds to Days, Hours, Minutes, Seconds
# thanks to https://www.shellscript.sh/tips/hms/
local seconds D H M S MM D_TAG H_TAG M_TAG S_TAG
seconds=${1:-0}
S=$((seconds%60))
MM=$((seconds/60)) # total number of minutes
M=$((MM%60))
H=$((MM/60))
D=$((H/24))
# set up "x day(s), x hour(s), x minute(s) and x second(s)" format
[ "$D" -eq "1" ] && D_TAG="day" || D_TAG="days"
[ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours"
[ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes"
[ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds"
# logic for handling display
[ "$D" -gt "0" ] && printf "%d %s " $D "${D_TAG},"
[ "$H" -gt "0" ] && printf "%d %s " $H "${H_TAG},"
[ "$seconds" -ge "60" ] && printf "%d %s " $M "${M_TAG} and"
printf "%d %s\n" $S "${S_TAG}"
}
duration=
howlong="$(rendertimer $duration)"
echo "That took ${howlong} to run."
期望的输出
349261
秒:"运行用了4天1小时1分1秒。"
127932
秒:"运行用了1天11小时32分12秒。"
86400
秒:“运行 花了 1 天时间。”
实际产量
349261
秒:"运行 花了 4 天 97 小时 1 分 1 秒。"
127932
秒:"运行用了1天35小时32分12秒。"
86400
秒:"运行用了1天24小时0分0秒。"
谁能帮我解决这个问题?
替换
H=$((MM/60))
D=$((H/24))
至
H=$((MM/60%24))
D=$((MM/60/24))
因此,天数是总分钟数的商,小时数是总分钟数的模数。
另外,你的第二个例子是错误的。 127932 的期望输出是 That took 1 day, 11 hours, 32 minutes and 12 seconds to run
感谢@BeardOverflow 和@NikolaySidorov,效果很好:
#!/bin/bash
rendertimer(){
# convert seconds to Days, Hours, Minutes, Seconds
# thanks to Nikolay Sidorov and https://www.shellscript.sh/tips/hms/
local parts seconds D H M S D_TAG H_TAG M_TAG S_TAG
seconds=${1:-0}
# all days
D=$((seconds / 60 / 60 / 24))
# all hours
H=$((seconds / 60 / 60))
H=$((H % 24))
# all minutes
M=$((seconds / 60))
M=$((M % 60))
# all seconds
S=$((seconds % 60))
# set up "x day(s), x hour(s), x minute(s) and x second(s)" language
[ "$D" -eq "1" ] && D_TAG="day" || D_TAG="days"
[ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours"
[ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes"
[ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds"
# put parts from above that exist into an array for sentence formatting
parts=()
[ "$D" -gt "0" ] && parts+=("$D $D_TAG")
[ "$H" -gt "0" ] && parts+=("$H $H_TAG")
[ "$M" -gt "0" ] && parts+=("$M $M_TAG")
[ "$S" -gt "0" ] && parts+=("$S $S_TAG")
# construct the sentence
result=""
lengthofparts=${#parts[@]}
for (( currentpart = 0; currentpart < lengthofparts; currentpart++ )); do
result+="${parts[$currentpart]}"
# if current part is not the last portion of the sentence, append a comma
[ $currentpart -ne $((lengthofparts-1)) ] && result+=", "
done
echo "$result"
}
duration=
howlong="$(rendertimer "${duration}")"
echo "That took ${howlong} to run."
我有一个函数,灵感来自 here 的 hms
,但我希望扩展它以包括处理和显示天数。
我已经开始编辑脚本,但很快意识到我无法处理将小时 运行 变成几天的逻辑,反之亦然...
这是我目前的情况:
#!/bin/bash
rendertimer(){
# convert seconds to Days, Hours, Minutes, Seconds
# thanks to https://www.shellscript.sh/tips/hms/
local seconds D H M S MM D_TAG H_TAG M_TAG S_TAG
seconds=${1:-0}
S=$((seconds%60))
MM=$((seconds/60)) # total number of minutes
M=$((MM%60))
H=$((MM/60))
D=$((H/24))
# set up "x day(s), x hour(s), x minute(s) and x second(s)" format
[ "$D" -eq "1" ] && D_TAG="day" || D_TAG="days"
[ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours"
[ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes"
[ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds"
# logic for handling display
[ "$D" -gt "0" ] && printf "%d %s " $D "${D_TAG},"
[ "$H" -gt "0" ] && printf "%d %s " $H "${H_TAG},"
[ "$seconds" -ge "60" ] && printf "%d %s " $M "${M_TAG} and"
printf "%d %s\n" $S "${S_TAG}"
}
duration=
howlong="$(rendertimer $duration)"
echo "That took ${howlong} to run."
期望的输出
349261
秒:"运行用了4天1小时1分1秒。"
127932
秒:"运行用了1天11小时32分12秒。"
86400
秒:“运行 花了 1 天时间。”
实际产量
349261
秒:"运行 花了 4 天 97 小时 1 分 1 秒。"
127932
秒:"运行用了1天35小时32分12秒。"
86400
秒:"运行用了1天24小时0分0秒。"
谁能帮我解决这个问题?
替换
H=$((MM/60))
D=$((H/24))
至
H=$((MM/60%24))
D=$((MM/60/24))
因此,天数是总分钟数的商,小时数是总分钟数的模数。
另外,你的第二个例子是错误的。 127932 的期望输出是 That took 1 day, 11 hours, 32 minutes and 12 seconds to run
感谢@BeardOverflow 和@NikolaySidorov,效果很好:
#!/bin/bash
rendertimer(){
# convert seconds to Days, Hours, Minutes, Seconds
# thanks to Nikolay Sidorov and https://www.shellscript.sh/tips/hms/
local parts seconds D H M S D_TAG H_TAG M_TAG S_TAG
seconds=${1:-0}
# all days
D=$((seconds / 60 / 60 / 24))
# all hours
H=$((seconds / 60 / 60))
H=$((H % 24))
# all minutes
M=$((seconds / 60))
M=$((M % 60))
# all seconds
S=$((seconds % 60))
# set up "x day(s), x hour(s), x minute(s) and x second(s)" language
[ "$D" -eq "1" ] && D_TAG="day" || D_TAG="days"
[ "$H" -eq "1" ] && H_TAG="hour" || H_TAG="hours"
[ "$M" -eq "1" ] && M_TAG="minute" || M_TAG="minutes"
[ "$S" -eq "1" ] && S_TAG="second" || S_TAG="seconds"
# put parts from above that exist into an array for sentence formatting
parts=()
[ "$D" -gt "0" ] && parts+=("$D $D_TAG")
[ "$H" -gt "0" ] && parts+=("$H $H_TAG")
[ "$M" -gt "0" ] && parts+=("$M $M_TAG")
[ "$S" -gt "0" ] && parts+=("$S $S_TAG")
# construct the sentence
result=""
lengthofparts=${#parts[@]}
for (( currentpart = 0; currentpart < lengthofparts; currentpart++ )); do
result+="${parts[$currentpart]}"
# if current part is not the last portion of the sentence, append a comma
[ $currentpart -ne $((lengthofparts-1)) ] && result+=", "
done
echo "$result"
}
duration=
howlong="$(rendertimer "${duration}")"
echo "That took ${howlong} to run."