Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2013 08:11
Show Gist options
  • Save anonymous/4516701 to your computer and use it in GitHub Desktop.
Save anonymous/4516701 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Jan 12, 2013.
    177 changes: 177 additions & 0 deletions exam.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,177 @@
    #!/bin/bash

    main() {
    clear

    while true
    do
    cat <<- MENU
    1) select
    2) command
    3) script
    4) makefile
    0) quit
    MENU
    read -p "please input your selection:" section
    echo
    case "$section" in
    1) selection;;
    2) command ;;
    3) script ;;
    4) makefile ;;
    0) exit 0 ;;
    *) echo "input error!" ;;
    esac
    done
    }

    selection() {
    paper="p1"
    grep "^#" $paper
    if [ ! -f 1.ans ]
    then
    gen_ans 1 20
    fi
    curr=1;
    while true
    do
    echo
    sed -n "/^$curr\./,/^$/p" $paper
    youranswer=`grep "^"$curr"\." 1.ans | cut -f2 -d" "`
    read -p "input your answer($youranswer): " answer
    case $answer in
    0)
    main
    ;;
    [1-9]|[1-2][0-9])
    if [ $curr -ge 1 -a $curr -le 20 ]; then
    curr="$answer"
    fi
    ;;
    [a-d]|[A-D])
    answer=`echo $answer | tr "[a-z]" "[A-Z]"`
    sed -i "s/^$curr\. .*/$curr\. $answer/" 1.ans
    if [ $curr -lt 20 ]; then
    curr=`expr $curr + 1`
    fi
    ;;
    -)
    if [ $curr -gt 1 ]; then
    curr=`expr $curr - 1`
    fi
    ;;
    "")
    if [ $curr -lt 20 ]; then
    curr=`expr $curr + 1`
    fi
    ;;
    *)
    echo "input the correct problem number or answer!"
    ;;
    esac
    done
    }

    command() {
    paper="p2"
    grep "^#" $paper
    curr=1;
    while true
    do
    echo
    sed -n "/^$curr\./,/^$/p" $paper
    echo -n "ANS: "
    if [ -f "$curr.cmd" ]
    then
    cat $curr.cmd 2> /dev/null
    else
    echo
    fi
    echo
    read -rp "input your answer: " answer
    case $answer in
    0)
    main
    ;;
    [1-9]|1[0-5])
    curr="$answer"
    ;;
    -)
    if [ $curr -gt 1 ]; then
    curr=`expr $curr - 1`
    fi
    ;;
    "")
    if [ $curr -lt 20 ]; then
    curr=`expr $curr + 1`
    fi
    ;;
    *)
    echo "$answer" > $curr.cmd
    if [ $curr -lt 15 ]; then
    curr=`expr $curr + 1`
    fi
    ;;
    esac
    done
    }

    script() {
    paper="p3"
    grep "^#" $paper
    curr=1;
    while true
    do
    echo
    sed -n "/^$curr\./,/^$/p" $paper
    read -p "input your selection(e to edit the file): " answer
    case $answer in
    0)
    main
    ;;
    [1-2])
    curr="$answer"
    ;;
    -)
    if [ $curr -gt 1 ]; then
    curr=`expr $curr - 1`
    fi
    ;;
    e)
    vi $curr.sh
    ;;
    "")
    if [ $curr -lt 2 ]; then
    curr=`expr $curr + 1`
    fi
    ;;
    *)
    echo "input the correct number"
    ;;
    esac
    done
    }

    makefile() {
    paper="p4"
    cat $paper
    read -p "press 0 to quit or any other key to start edit the makefile...: " choice
    if [ $choice = "0" ]; then
    main
    else
    vi makefile
    main
    fi
    }

    gen_ans() {
    > $1.ans
    cur=1
    while [ $cur -le $2 ]
    do
    echo "$cur. " >> $1.ans
    cur=`expr $cur + 1`
    done
    }

    main