randint.sh (1445B)
1 #!/bin/sh 2 3 c=0 4 challenge=1 5 flat=0 6 range="1-100" 7 8 OPTS="hc:fr:" 9 LONGOPTS="help,challenge:,flat,range:" 10 11 parsed=$(getopt --options=$OPTS --longoptions=$LONGOPTS -- "$@") 12 eval set -- "${parsed[@]}" 13 14 while true; do 15 case "$1" in 16 -f|--flat) flat=1 ;; 17 -c|--challenge) 18 c=1 19 challenge=$2 20 shift;; 21 -r|--range) 22 range="$2" 23 shift;; 24 25 --) # end of arguments 26 shift 27 break 28 ;; 29 30 *) 31 printf '%sn' "Error while parsing CLI options" 1>&2 32 ;; 33 esac 34 35 shift 36 done 37 38 successes=0 39 from=0 40 til=0 41 average=0 42 43 if [ "$c" == "1" ]; then 44 clear 45 for i in {3..1}; do 46 echo "$i" 47 sleep 1 48 clear 49 done 50 fi 51 52 for i in $(seq $challenge); do 53 from=$(date "+%s") 54 55 rand=$(shuf -i "$range" -n 1) 56 echo $rand | sed ':a;s/B[0-9]{3}>/,&/;ta' 57 58 word=$(python ~/.local/bin/inttoesp.py "$rand") 59 60 read user_input 61 62 if [ "$word" == "$user_input" ]; then 63 echo "correcto!" 64 successes=$((successes + 1)) 65 else 66 actual="" 67 for (( j=0; j<${#word}; j++ )); do 68 if [ "${word:$j:1}" == "${user_input:$j:1}" ]; then 69 actual="$actual${word:$j:1}" 70 else 71 actual="$actuale[01;31m${word:$j:1}e[0m" 72 fi 73 done 74 echo -e $actual 75 fi 76 77 til=$(date "+%s") 78 average=$((average + (til - from))) 79 done 80 81 if [ "$c" == "1" ]; then 82 echo "average time: $((average/challenge)) s" 83 echo "total time: $average s" 84 echo "total: $successes / $challenge correct!" 85 fi