时间上传部分 bash 脚本并在 MM:SS 中输出

Time upload part of bash script and output in MM:SS

我有一个将文件上传到服务器的功能,我想测量脚本的这一部分需要多长时间并显示结果以供输出使用, 例如:

echo "$file uploaded in XXXM:XXs"

我可以显示秒数,有:

my_upload_stuff here
echo "$file uploaded in $SECONDS" 

据我所知,它显示自脚本启动以来的秒数(这对我的需要来说很好)但这是我所能得到的。

拔掉我剩余的头发试图解决这个问题,似乎比我想象的要难得多。到处都是房子,但似乎没有什么用 - 我承认我的 bash 技能是新手...

如果已安装,您可以使用命令 time(但您必须使用外部命令 /usr/bin/time 和所以你必须安装它,如果它还没有安装......因为内置的 bash 命令 time 没有产生所需输出的选项) .

或者,您可以显式地执行计算,如下所示:

t1=$(date +%s)
<put upload_command here>
t2=$(date +%s)
echo "Time elapsed for upload: $(( ( t2 - t1 ) / 60 )) minutes and $(( ( t2 - t1 ) % 60 )) seconds"