#!/bin/bash ###################################### # source-encoding >> UTF-8 # # 数当て # author: 河合 佑太  ####################################### flag=0 #リダイレクトとコンソール出力を同時に行う。 #引数で渡された文字列が対象となる function outputFunc() { echo $1 if test $flag -eq 1; then echo $1 >> quiz1_output.txt else echo $1 > quiz1_output.txt flag=1 fi } #コンソール画面で入力したものがリダイレクトされる。 #tpNum(仕様上グロバール変数)を参照すれば関数外部でもreadしたものを知ることができる。 function readFunc() { read tpNum if test $flag -eq 1; then echo "$tpNum" >> quiz1_output.txt else echo "$tpNum" > quiz1_output.txt flag=1 fi return } #引数を変数に保存 answerNumber=$1 #何回入力したかをカウントするための変数を1に初期化 counter=1 #出力を通して、数の入力を促す outputFunc 'Please type the number expected the answer.' outputFunc '?' #入力値を変数typeNumberに格納 readFunc typeNumber=$tpNum #入力された値と数当ての値が一致するまでループを回す while [ $answerNumber -ne $typeNumber ] do #入力値と数当ての値を大小比較して結果を出力 if test $answerNumber -le $typeNumber; then outputFunc "The typed number '$typeNumber' is larger than the answer!!" else outputFunc "The typed number '$typeNumber' is smaller than the answer!!" fi #まだ数当ての値と一致していないので再び入力を促す outputFunc ' ' outputFunc '---------------------------------------------------------------' outputFunc 'Please type the number expected the answer again.' outputFunc '?' readFunc typeNumber=$tpNum #カウンタをインクリメントする counter=`expr $counter + 1` done #数当てに正解したら最後に、何回入力したかを出力して終了する #outputFunc "" outputFunc " +*******************************************************+" outputFunc "Congratulation!! You have typed the answer '$answerNumber'" if test $counter -le 1; then outputFunc "(Total: $counter time)" else outputFunc "(Total: "$counter" times)" fi outputFunc '+*****************************************************+'