Created
February 22, 2021 05:54
-
-
Save fabiolimace/ac3f728a8873cbe3ba9e070ef559d260 to your computer and use it in GitHub Desktop.
Cronometro simples feito para o bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Cronometro simples feito em Shell. | |
| # Script criado apenas para fins de estudo. | |
| # Para parar, use um [ctrl] + C. | |
| # Autor: Fabio Lima | |
| # Data: 2009-02-14 | |
| # Inicializacao das variaveis | |
| SS=0; | |
| MM=0; | |
| HH=0; | |
| # Inicio do 'loop infinito' que atualizara | |
| # as variaveis a cada segundo. | |
| # A condicao 'sleep 1' se encarrega de gerar | |
| # atrasos de um segundo a cada passagem. | |
| while (sleep 1) | |
| do | |
| # Testa a quantidade de segundos | |
| # Se SS > 59 entao MM++ | |
| if ( test $SS -gt 59) | |
| then | |
| SS=0 | |
| MM=$(echo "$MM + 1" | bc) | |
| fi | |
| # Testa a quantidade de minutos | |
| # Se MM > 59 entao HH++ | |
| if (test $MM -gt 59) | |
| then | |
| MM=0 | |
| HH=$(echo "$HH + 1" | bc) | |
| fi | |
| # Testa a quantidade de horas | |
| # Se HH > 23 entao Zera todas variaveis | |
| if (test $HH -gt 23) | |
| then | |
| MM=0 | |
| HH=0 | |
| HH=0 | |
| fi | |
| # Mostra o tempo decorrido depois de atualizado | |
| # Opcoes do 'echo': | |
| # -n: desabilitar nova linha no final do comando (\n) | |
| # -e: habilitar o uso de caracteres de scape (\t, \n, \a \r etc) | |
| echo -n -e "\rTempo decorrido: $HH h $MM min $SS seg " | |
| # Acrescenta um segundo | |
| # O 'bc' eh uma realiza calculos com inteiros | |
| SS=$(echo "$SS + 1" | bc) | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment