How do I measure duration in seconds in a shell script?

前端 未结 7 536
感情败类
感情败类 2020-12-07 22:02

I wish to find out how long an operation takes in a Linux shell script. How can I do this?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 22:23

    Here is the script to find the time elapsed in milliseconds. Replace the sleep 60 line with the code you want to execute.

    a=0
    while [ $a -lt 10 ]
    do
    START_TIME=`echo $(($(date +%s%N)/1000000))`
    sleep 3
    END_TIME=`echo $(($(date +%s%N)/1000000))`
    ELAPSED_TIME=$(($END_TIME - $START_TIME))
    echo $ELAPSED_TIME
    if [ $a -eq 10 ]
    then
      break
    fi
    a=`expr $a + 1`
    done
    

提交回复
热议问题