平面设计手绘网站,网络营销推广seo,大数据营销的缺点,photoshop手机版安卓我们在使用shell脚本进行一些批量活动的时候#xff0c;在有的场景下会需要知道脚本执行用了多长的时间#xff0c;一谈到这个话题#xff0c;我们一般的想法就是记录时间再开始阶段#xff0c;执行完成后再记录时间#xff0c;然后求时间差#xff0c;这样是可以的…我们在使用shell脚本进行一些批量活动的时候在有的场景下会需要知道脚本执行用了多长的时间一谈到这个话题我们一般的想法就是记录时间再开始阶段执行完成后再记录时间然后求时间差这样是可以的但是要进行格式的转换比较麻烦今天我们使用一个简单的方法。 要执行的脚本内容
#!/bin/sh
started$SECONDS
#清除缓存
#sh -c sync echo 3 /proc/sys/vm/drop_caches
dd if/dev/zero of/home/forlinx/test/test.bin bs64k count1000 convfsync oflagdirect
if [ $? -eq 0 ]
then echo emmc write $i succesful | tee -a ./result.log
elseecho emmc write $i failure | tee -a ./result.log
fi
sleep 2s
echo
#dd if/dev/zero of$a bs16k count1000 convsync iflagdirect
#sh -c sync echo 3 /proc/sys/vm/drop_caches
dd if/home/forlinx/test/test.bin of/dev/null bs64k count1000 convsync iflagdirect
if [ $? -eq 0 ]
thenecho emmc read $i succesful | tee -a ./result.log
elseecho emmc read $i failure | tee -a ./result.log
fi
sleep 2s
echo
echo runtimes $(($SECONDS-$started)) seconds..
注意看我们再开始定义了一个变量started使用全局变量$SECONDS来赋值。 这个全局变量是时刻记录当前的时间的而不用我们使用命令来获取就是说系统默认就有一个时刻走的时钟我们仅需要计算一次就OK了。 执行完成后使用当前的时钟减去我们记录的时钟就OK了注意单位是秒 注意使用这个方法需要再执行脚本的时候有注意事项如下 1.普通方法执行。
forlinxubuntu:~/test$ ./emmc.sh
10000 records in
10000 records out
65536000 bytes (66 MB, 62 MiB) copied, 0.0855573 s, 766 MB/s
emmc write succesful 10000 records in
10000 records out
65536000 bytes (66 MB, 62 MiB) copied, 0.0765326 s, 856 MB/s
emmc read succesful ./emmc.sh: 25: ./emmc.sh: arithmetic expression: expecting primary: -
2.正确方法 使用time bash 加脚本名称如下
forlinxubuntu:~/test$ time bash emmc.sh
10000 records in
10000 records out
65536000 bytes (66 MB, 62 MiB) copied, 0.107433 s, 610 MB/s
emmc write succesful 10000 records in
10000 records out
65536000 bytes (66 MB, 62 MiB) copied, 0.0898149 s, 730 MB/s
emmc read succesful runtimes 4 seconds..real 0m4.207s
user 0m0.006s
sys 0m0.137s