---
# System prepended metadata

title: 3-1-other

---

## 3-1-6
```
#!/bin/bash
FUNADD(){
    if [ $1 -eq "0" ] ;then
        exit 1
    fi
    echo "total is: $1"
    varAvg=$(echo "scale=2; $1 / $2" | bc)
    echo "average=$varAvg"
}
varNum=0
varinp=""
varTime=0
varAvg=0
until [ $varInp == "END" ] 2> /dev/null
do
    read -p "input a number: " varInp
    [ -z $varInp ] 2> /dev/null && echo "please enter some number." && continue
    if [ $varInp == "END" ]
    then
        continue
    fi
    ! [ $varInp -eq $varInp ] 2> /dev/null && echo "please enter number." && continue
    varNum=$[varNum+varInp]
    varTime=$[varTime+1]
    echo "$varTime"
done
    FUNADD $varNum $varTime
exit  0

```

## 3-1-?
```
GradesEvaluation(){
    if [ $1 -eq "0" ] ;then
        exit 1
    fi
    total1=$(echo "scale=2;$1  / ($1 + $2) * 100 " | bc)
    total2=$(echo "scale=2;$2  / ($1 + $2) * 100 " | bc)

    total1=$(echo "$total1" | cut -d '.' -f1)
    total2=$(echo "$total2" | cut -d '.' -f1)

    echo "Passing average: $total1 %"
    echo "Failing average: $total2 %"
#印這個迴圈，範為從0到(最後-1)
    echo "${grade[@]:0:$((${#grade[@]}-1))}"
}

grade[0]=0
passgrade=60
PCount=0
FCount=0
# for loop counting
i=0

until [ ${grade[$i]} == "END" ] 2> /dev/null
do
    read -p "請輸入學生成績（或輸入 'END' 結束輸入）: " grade[i]
    [ -z $grade ] 2> /dev/null && echo "please enter some number." && continue
    if [ "${grade[$i]}" == "END" ]
    then
         continue
    fi
    ! [ $grade -eq $grade ] 2> /dev/null && echo "please enter number." && continue

    until [ ${grade[$i]} -lt $passgrade ]
    do
        PCount=$[PCount+1]
        break
    done
    while [ ${grade[$i]} -lt $passgrade ]
    do
        FCount=$[FCount+1]
        break
    done
    let i=$i+1
done
GradesEvaluation $PCount $FCount

exit 0

```

## 3-1-12
請寫一個發票紀錄程式，使用者輸入發票號碼後，檢查發票為AA11111111共十碼無誤後，存進純文字檔 (需使用到 while, function, break)。

```
#!/bin/bash
check_invoice() {
    if [[ $1 =~ ^[A-Z]{2}[0-9]{8}$ ]]
    then
        return 0
    else
        return 1
    fi
}

echo "When all invoices have been entered, please enter 'exit' to leave. "

while true
do
    read -p "Please enter invoice numbers: " invoice_number
    if [ "$invoice_number" == "end" ] 2> /dev/null
    then
        echo "complete."
        break
    fi
#Forfun
    if [ "$invoice_number" == "cat" ] 2> /dev/null
    then
        touch ./invoice.txt
        cat ./invoice.txt
        echo ""
        continue
    fi

    if [ "$invoice_number" == "clean" ] 2> /dev/null
    then
        rm ./invoice.txt
        echo -e "complete!\n "
        continue
    fi
#forFun
    if check_invoice "$invoice_number"
    then
        echo -e "invoices saved successfully.\n"
        echo "$invoice_number" >> invoice.txt
    else
        echo -e "invoice number invalid, please enter again!\n"
    fi
done

```

## 3-1-12 + 13...?!
```
#!/bin/bash
check_invoice() {
    if [[ $1 =~ ^[A-Z]{2}[0-9]{8}$ ]]
    then
        return 0
    else
        return 1
    fi
}

check_won_or_not() {
    while read rows1
    do
        while read rows2
        do
            price_number=$(echo "$rows2" | cut -d '.' -f1)
            invoice_number=$(echo "$rows2" | cut -d '.' -f2)
            if [ "$rows1" == "$invoice_number" ]
            then
                echo "$rows1 won $price_number price!"
                break
            elif [ "$rows2" == "end" ]
            then
                echo "$rows1 make persistent efforts "
            fi
        done < won.txt
    done < invoice.txt
}

echo "When nothing to do, please enter 'end' to leave. "

until false
do
    read -p "Please enter what do you want to do or invoice numbers: " invoice_number
    echo ""cat" to echo all the invoices; "clean" to clean up all the invoices; "check"; "build""
    if [ "$invoice_number" == "end" ] 2> /dev/null
    then
        echo "complete."
        break

    elif [ "$invoice_number" == "cat" ] 2> /dev/null
    then
        touch invoice.txt
        cat invoice.txt
        echo ""
        continue

    elif [ "$invoice_number" == "clean" ] 2> /dev/null
    then
        rm invoice.txt
        echo -e "complete!\n "
        continue

    elif [ "$invoice_number" == "check" ] 2> /dev/null
    then
        check_won_or_not
           
    elif [ "$invoice_number" == "build" ] 2> /dev/null
    then
        echo "" > won.txt
        while true
        do
            read -p "Please enter price number: " price_number
            if [ "$price_number" == "end" ]
            then
                echo "build complete."
                echo "$price_number" >> won.txt
                break
            fi
            read -p "Please enter invoice number: " invoice_number
            if check_invoice "$invoice_number"
            then
                echo "$price_number.$invoice_number" >> won.txt
            else
                echo -e "wrong invoice number!\n"
            fi
        done

    elif check_invoice "$invoice_number"
    then
        echo -e "invoices saved successfully.\n"
        echo "$invoice_number" >> invoice.txt
    else
        echo -e "invoice number invalid, please enter again!\n"
    fi
done
 
```