sober (853B)
1 #!/bin/sh 2 # Simple sobriety checker inspired by DrunkGuard. 3 4 NCHECKS=3 5 DRUNKSFILE=/tmp/drunks 6 7 die() { 8 echo "$@" 9 exit 1 10 } 11 12 rnd() { 13 nofd="$1" # n. of digits 14 cset="$2" # character set 15 [ -z "$nofd" ] && nofd=1 16 [ -z "$cset" ] && cset="[:digit:]" 17 dd if=/dev/urandom of=/dev/stdout bs=4096 count=1 2>/dev/null |tr -dc "$cset" |head -c "$nofd" 18 } 19 20 check() { 21 n1="$(rnd)" 22 n2="$(rnd)" 23 sum="$(echo "${n1}+${n2}" | bc)" 24 printf "How much is %d + %d? " "$n1" "$n2" 25 read r 26 nan="$(echo "$r" | grep -c '[^0-9]')" 27 [ "$nan" -eq 1 ] && return 1 28 [ "$r" -ne "$sum" ] && return 1 29 return 0 30 } 31 32 drunk() { 33 echo "$(date +'%d/%m/%Y %H:%M:%S') | $(id -un) | $@" >> "$DRUNKSFILE" 34 die "You're drunk, I'll take care of you." 35 } 36 37 main() { 38 [ "$#" -eq 0 ] && die "Are you sober?" 39 for i in $(seq 1 $NCHECKS); do 40 check 41 [ $? -ne 0 ] && drunk "$@" 42 done 43 $@ 44 } 45 46 main "$@"