All pastes #394576 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-14 12:55 UTC
rendered paste body
#!/bin/sh
PIERROR=~/.pierror
# pi gotten from the web page.

pv=(3.1415 92653 58979 32384 62643 38327 95028 84197 16939 93751 05820 97494 45923 07816 40628
62089 98628 03482 53421 17067 98214 80865 13282 30664 70938 44609 55058 22317 25359 40812 84811
17450 28410 27019 38521 10555 96446 22948 95493 03819 64428 81097 56659 33446 12847 56482 33786
78316 52712 01909 14564 85669 23460 34861 04543 26648 21339 36072 60249 14127 37245 87006 60631
55881 74881 52092 09628 29254 09171 53643 67892 59036 00113 30530 54882 04665 21384 14695 19415
11609 43305 72703 65759 59195 30921 86117 38193 26117 93105 11854 80744 62379 96274 95673 51885
75272 48912 27938 18301 19491 29833 67336 24406 56643 08602 13949 46395 22473 71907 02179 86094
37027 70539 21717 62931 76752 38467 48184 67669 40513 20005 68127 14526 35608 27785 77134 27577
89609 17363 71787 21468 44090 12249 53430 14654 95853 71050 79227 96892 58923 54201 99561 12129
02196 08640 34418 15981 36297 74771 30996 05187 07211 34999 99983 72978 04995 10597 31732 81609
63185 95024 45945 53469 08302 64252 23082 53344 68503 52619 31188 17101 00031 37838 75288 65875
33208 38142 06171 77669 14730 35982 53490 42875 54687 31159 56286 38823 53787 59375 19577 81857
78053 21712 26806 61300 19278 76611 19590 92164 20198 93809 52572 01065 48586 32788 65936 15338
18279 68230 30195 20353 01852 96899 57736 22599 41389 12497 21775 28347 91315 15574 85724 24541
50695 95082 95331 16861 72785 58890 75098 38175 46374 64939 31925 50604 00927 70167 11390 09848
82401 28583 61603 56370 76601 04710 18194 29555 96198 94676 78374 49448 25537 97747 26847 10404
75346 46208 04668 42590 69491 29331 36770 28989 15210 47521 62056 96602 40580 38150 19351 12533
82430 03558 76402 47496 47326 39141 99272 60426 99227 96782 35478 16360 09341 72164 12199 24586
31503 02861 82974 55570 67498 38505 49458 85869 26995 69092 72107 97509 30295 53211 65344 98720
27559 60236 48066 54991 19881 83479 77535 66369 80742 65425 27862 55181 84175 74672 89097 77727
93800 08164 70600 16145 24919 21732 17214 77235 )

nd=${#pv[@]};

if [ ! $1 ]; then
    sv=0
else
    sv=$1
fi

i=$((sv/5));
# While we still have digits left,
while [ $i -lt $nd ]; do
    read gv;
    chv=`echo $gv | sed 's/^0*//;s/\.//'`
    if [ ! $chv ]; then chv=0; fi
    # Did the person type nothing?
    if [ ! $gv ]; then
        if [ $sv -eq 0 ] ; then
            cat - <<EOF
This is the pi program.
Enter in sequences of 5 digits of pi.  If you make a mistake,
you'll be told what the correct 5 digit sequence is, and start
over.

This program may be called as $0 <num>, which will start at the <num>th
decimal point.

If a negative value is entered, the digits of pi up to that number of digits
will be displayed.

EOF
        else
            echo ${pv[$i-1]}
        fi
        # Was what they typed a number?
    elif ! echo $gv | perl -ne 'exit 1 unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;' ; then
        case $gv in
            clear ) clear
            ;;
            plot )
            gnuplot - <<EOF
            set term dumb $COLUMNS 30
            set ylabel "digits"
            plot "$PIERROR" using 0:(\$1*5) t "" w l
EOF
            ;;
            quit )
            exit
            ;;
            * )
            cat <<EOF
Available commands are:
plot
quit
clear

If you type in a number less than zero, the absolute value of it
will be the number of digits that are displayed.

EOF
        esac

        # Was it negative?
    elif [ $chv -lt 0 ]; then
        chv=$((chv*-1))
        cnt=0
        for ((r=0;r<(chv/5);r++)); do
            echo -n ${pv[$r]}' '
            cnt=$((cnt+1))
            if ((!(cnt%10))) ; then echo ; fi
        done
        echo
        # Is it correct?
    elif [ ${pv[$i]} == $gv ]; then
        ((i++));
  else #If it's wrong, correct them.
        echo 'WRONG-->'${pv[$i]};
        echo $i >> ~/.pierror
        i=$((sv/5));
        sleep 5;
        clear;
    fi;
    # Every so often, we should tell them how far they've gotten.
    if ((!(i%10))); then
        echo '---' $((i*5)) '---' ;
    fi ;
done