Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # DeltaT
- # - Contagem de tempo entre processos
- # - Precisão de segundos
- # - Pode ser usado para contagem "aninhada" de tempo
- # - Aceita uma quantidade "offset" opcional de segundos
- # - Devolve:
- # a hora e o timestamp de início e fim da contagem
- # a variação de tempo em hh:mm:ss e em segundos
- #
- # Arkanon <arkanon@lsd.org.br>
- # 2010/03/18 Qui 15:54:42 BRS
- #
- # EXEMPLO
- #
- # Um script com:
- #
- # time_1=$(deltat start)
- #
- # echo "sleeping 3s..."
- # sleep 3
- #
- # time_2=$(deltat start)
- # read -p "prompt: " p
- # echo -e "\nyou said: $p\n"
- # time_2=$(deltat stop "$time_2")
- # echo -e "$time_2\n"
- #
- # time_1=$(deltat stop "$time_1")
- # echo -e "$time_1"
- #
- # Devolve:
- #
- # sleeping 3s...
- # prompt: oi
- #
- # you said: oi
- #
- # OFFSET 0
- # START [2010/10/01 Sex 18:00:25 BRS] 1285966825
- # END [2010/10/01 Sex 18:00:26 BRS] 1285966826
- # DELTA 00:00:01 1
- #
- # OFFSET 0
- # START [2010/10/01 Sex 18:00:22 BRS] 1285966822
- # END [2010/10/01 Sex 18:00:26 BRS] 1285966826
- # DELTA 00:00:04 4
- fmt="[%Y/%m/%d %a %H:%M:%S %Z]\t%s"
- now=$(date +'%m/%d/%Y %H:%M:%S')
- ts=$(date -d "$now" +"%s")
- point=$1
- if [ "$point" = "start" ] && ( [ $# = 1 ] || [ $# = 2 ] )
- then
- [ $# = 2 ] && offset=$2 || offset=0
- echo -e "OFFSET $offset"
- echo -e " START "$(LANG=pt_BR date -d "$now" +"$fmt")
- else
- offset=$(echo "$2" | grep "OFFSET" | cut -f2)
- ts0=$(echo "$2" | grep "START" | cut -f3)
- if [ "$point" = "stop" ] && [ $# = 2 ] && [ "$ts" -ge "$ts0" ]
- then
- diffts=$[$ts-$ts0+$offset]
- time_t=$diffts
- time_s=$[$time_t%60]
- time_t=$[$time_t/60]
- time_m=$[$time_t%60]
- time_h=$[$time_t/60]
- [ $time_s -le 9 ] && time_s="0$time_s"
- [ $time_m -le 9 ] && time_m="0$time_m"
- [ $time_h -le 9 ] && time_h="0$time_h"
- delta="$time_h:$time_m:$time_s"
- echo "$2"
- echo -e " END "$(LANG=pt_BR date -d "$now" +"$fmt")
- echo " DELTA $delta $diffts"
- else
- echo 'Usage: var=`deltat start [ts offset]`; cmd[; cmd; ...]; var=`deltat stop "$var"`; echo $var'
- fi
- fi
- # [EOF]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement