Bash script to calculate time elapsed

后端 未结 10 1447
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 22:03

I am writing a script in bash to calculate the time elapsed for the execution of my commands, consider:

STARTTIME=$(date +%s)
#command block that takes time to c         


        
相关标签:
10条回答
  • 2021-01-29 22:30
        #!/bin/bash
    
        time_elapsed(){
        appstop=$1; appstart=$2
    
        ss_strt=${appstart:12:2} ;ss_stop=${appstop:12:2}
        mm_strt=${appstart:10:2} ;mm_stop=${appstop:10:2}
         hh_strt=${appstart:8:2} ; hh_stop=${appstop:8:2}
         dd_strt=${appstart:6:2} ; dd_stop=${appstop:6:2}
         mh_strt=${appstart:4:2} ; mh_stop=${appstop:4:2}
         yy_strt=${appstart:0:4} ; yy_stop=${appstop:0:4}
    
        if [ "${ss_stop}" -lt "${ss_strt}" ]; then ss_stop=$((ss_stop+60)); mm_stop=$((mm_stop-1)); fi
        if [ "${mm_stop}" -lt "0" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
        if [ "${mm_stop}" -lt "${mm_strt}" ]; then mm_stop=$((mm_stop+60)); hh_stop=$((hh_stop-1)); fi
        if [ "${hh_stop}" -lt "0" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
        if [ "${hh_stop}" -lt "${hh_strt}" ]; then hh_stop=$((hh_stop+24)); dd_stop=$((dd_stop-1)); fi
    
        if [ "${dd_stop}" -lt "0" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
        if [ "${dd_stop}" -lt "${dd_strt}" ]; then dd_stop=$((dd_stop+$(mh_days $mh_stop $yy_stop))); mh_stop=$((mh_stop-1)); fi
    
        if [ "${mh_stop}" -lt "0" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
        if [ "${mh_stop}" -lt "${mh_strt}" ]; then mh_stop=$((mh_stop+12)); yy_stop=$((yy_stop-1)); fi
    
        ss_espd=$((10#${ss_stop}-10#${ss_strt})); if [ "${#ss_espd}" -le "1" ]; then ss_espd=$(for((i=1;i<=$((${#ss_stop}-${#ss_espd}));i++)); do echo -n "0"; done; echo ${ss_espd}); fi
        mm_espd=$((10#${mm_stop}-10#${mm_strt})); if [ "${#mm_espd}" -le "1" ]; then mm_espd=$(for((i=1;i<=$((${#mm_stop}-${#mm_espd}));i++)); do echo -n "0"; done; echo ${mm_espd}); fi
        hh_espd=$((10#${hh_stop}-10#${hh_strt})); if [ "${#hh_espd}" -le "1" ]; then hh_espd=$(for((i=1;i<=$((${#hh_stop}-${#hh_espd}));i++)); do echo -n "0"; done; echo ${hh_espd}); fi
        dd_espd=$((10#${dd_stop}-10#${dd_strt})); if [ "${#dd_espd}" -le "1" ]; then dd_espd=$(for((i=1;i<=$((${#dd_stop}-${#dd_espd}));i++)); do echo -n "0"; done; echo ${dd_espd}); fi
        mh_espd=$((10#${mh_stop}-10#${mh_strt})); if [ "${#mh_espd}" -le "1" ]; then mh_espd=$(for((i=1;i<=$((${#mh_stop}-${#mh_espd}));i++)); do echo -n "0"; done; echo ${mh_espd}); fi
        yy_espd=$((10#${yy_stop}-10#${yy_strt})); if [ "${#yy_espd}" -le "1" ]; then yy_espd=$(for((i=1;i<=$((${#yy_stop}-${#yy_espd}));i++)); do echo -n "0"; done; echo ${yy_espd}); fi
    
        echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}"
        #return $(echo -e "${yy_espd}-${mh_espd}-${dd_espd} ${hh_espd}:${mm_espd}:${ss_espd}")
        }
    
        mh_days(){
        mh_stop=$1; yy_stop=$2; #also checks if it's leap year or not
    
        case $mh_stop in
         [1,3,5,7,8,10,12]) mh_stop=31
         ;;
         2) (( !(yy_stop % 4) && (yy_stop % 100 || !(yy_stop % 400) ) )) && mh_stop=29 || mh_stop=28
         ;;
         [4,6,9,11]) mh_stop=30
         ;;
        esac
    
        return ${mh_stop}
        }
    
        appstart=$(date +%Y%m%d%H%M%S); read -p "Wait some time, then press nay-key..." key; appstop=$(date +%Y%m%d%H%M%S); elapsed=$(time_elapsed $appstop $appstart); echo -e "Start...: ${appstart:0:4}-${appstart:4:2}-${appstart:6:2} ${appstart:8:2}:${appstart:10:2}:${appstart:12:2}\nStop....: ${appstop:0:4}-${appstop:4:2}-${appstop:6:2} ${appstop:8:2}:${appstop:10:2}:${appstop:12:2}\n$(printf '%0.1s' "="{1..30})\nElapsed.: ${elapsed}"
    
        exit 0
    
    
    -------------------------------------------- return
    Wait some time, then press nay-key...
    Start...: 2017-11-09 03:22:17
    Stop....: 2017-11-09 03:22:18
    ==============================
    Elapsed.: 0000-00-00 00:00:01
    
    0 讨论(0)
  • Either $(()) or $[] will work for computing the result of an arithmetic operation. You're using $() which is simply taking the string and evaluating it as a command. It's a bit of a subtle distinction. Hope this helps.

    As tink pointed out in the comments on this answer, $[] is deprecated, and $(()) should be favored.

    0 讨论(0)
  • 2021-01-29 22:31

    You are trying to execute the number in the ENDTIME as a command. You should also see an error like 1370306857: command not found. Instead use the arithmetic expansion:

    echo "It takes $(($ENDTIME - $STARTTIME)) seconds to complete this task..."
    

    You could also save the commands in a separate script, commands.sh, and use time command:

    time commands.sh
    
    0 讨论(0)
  • 2021-01-29 22:35

    try using time with the elapsed seconds option:

    /usr/bin/time -f%e sleep 1 under bash.

    or \time -f%e sleep 1 in interactive bash.

    see the time man page:

    Users of the bash shell need to use an explicit path in order to run the external time command and not the shell builtin variant. On system where time is installed in /usr/bin, the first example would become /usr/bin/time wc /etc/hosts

    and

    FORMATTING THE OUTPUT
    ...
        %      A literal '%'.
        e      Elapsed  real  (wall  clock) time used by the process, in
                     seconds.
    
    0 讨论(0)
提交回复
热议问题