更新#2
你的脚本有一些问题。首先,`cat file`您应该使用 `<file`or 而不是$(<file). 只需打开文件即可免去一fork通电话。另一方面,调用and (and ) 也不需要,因为内部具有适当的功能。因此,您可以再次节省一些s 和s。execbashcutbcprintfbashforkexec
如果输入文件很大(大于 cca 32 KiB),则for-loop 行可能太大而无法处理,bash因此我建议改用while-loop 并逐行读取文件。
我可以在纯bash(应用 Atle 的 substr 解决方案)中提出类似的建议:
while IFS=: read hr min; do
incr_min=$((1$min+1)); #Octal problem solved
echo ${incr_min: -2}; #Mind the space before -2!
#or with glennjackman's suggestion to use decimal base
#incr_min=0$((10#$min+1))
#echo ${incr_min: -2};
#or choroba's solution improved to set variable directly
#printf -v incr_min %02d $((10#$min+1))
#echo $incr_min
done <file
输入文件
$ cat file
2:43
2:05
15:00
12:07
12:08
12:09
输出:
44
06
01
08
09
10
也许这printf -v是最简单的,因为它只需一步即可将结果放入变量中。
tripleee如果结果为 60 会发生什么,这是一个很好的问题。