Bash shell script for 100 iterations and log time taken

后端 未结 5 1295
长发绾君心
长发绾君心 2020-12-15 07:27

How do I run 100 iterations using a bash shell script? I want to know how long it will take to execute one command (start and end time). I want to keep track which iteratio

5条回答
  •  粉色の甜心
    2020-12-15 07:58

    for ((i = 1; i <= 100; i++)); do
        echo "--- Iteration #$i: $(date) ---"
        time command1
    done 2>&1 | tee timing.log
    

    There's a lot going on here. What's happening?

    1. The for loop iterates from 1 to 100 using C-style syntax.
    2. The $i in the echo printout prints the current iteration number.
    3. $(date) inserts a timestamp into each printout.
    4. The time command runs a command and prints how long it took to execute.
    5. The output from everything inside of the loop is piped to tee, which saves a copy to timing.log.
    6. The 2>&1 redirects stderr to stdout so that the log file will contain both regular output and error messages.

提交回复
热议问题