第三章:Creating Utilities--24.インタラクティブな計算機
以前は9番目のスクリプトを書いていましたが、コマンドラインがbcを呼び出して浮動小数点計算を行うことができます.そのため、コマンドラインベースの計算機パッケージスクリプトをインタラクティブに書く必要があります.ヘルプ情報を加えても短いという利点があります.
スクリプトの実行方法:このスクリプトの最も興味深い部分は、そのwhileループかもしれません.ユーザーが入力を完了するまでcalc>のプロンプトを作成します.もちろん、このスクリプトの間接性はそれ自身を達成しました.shellスクリプトは特に複雑ではありません.
≪スクリプトの実行|Run Script|emdw≫:このスクリプトは、インタラクティブなため、特定の操作を完了するようユーザーに促すことができるため、走るのが簡単です.パラメータが渡されるとscripbcに渡されます.sh.
実行結果:
#!/bin/sh
# calc.sh -- bc
scale=2
show_help()
{
cat << EOF
In addition to standard math function, calc also supports
a % b remainder of a/b
a ^ b exponential: a raised to the b power
s(x) sine of x, x in radians
c(x) cosine of x, x in radians
a(x) actangent of x, returns radians
l(x) natural log of x
e(x) exponential log of raising e to the x
j(n, x) bessel function of integer order n of x
scale N show N fractional digits(default = 2)
EOF
}
if [ $# -gt 0 ]; then
exec scriptbc.sh "$@"
fi
echo "Calc - a simple calculator. Enter 'help' for help, 'quit' to quit."
echo -n "calc> "
while read command args # Python
do
case $command in
quit|exit) exit 0;;
help|\?) show_help;;
scale) scale=$args;;
*) scriptbc.sh -p $scale "$command" "$args";;
esac
echo -n "calc> "
done
echo ""
exit 0
スクリプトの実行方法:このスクリプトの最も興味深い部分は、そのwhileループかもしれません.ユーザーが入力を完了するまでcalc>のプロンプトを作成します.もちろん、このスクリプトの間接性はそれ自身を達成しました.shellスクリプトは特に複雑ではありません.
≪スクリプトの実行|Run Script|emdw≫:このスクリプトは、インタラクティブなため、特定の操作を完了するようユーザーに促すことができるため、走るのが簡単です.パラメータが渡されるとscripbcに渡されます.sh.
実行結果:
calc 150 / 3.5
42.85
./calc.sh
Calc - a simple calculator. Enter 'help' for help, 'quit' to quit.
calc> help
In addition to standard math function, calc also supports
a % b remainder of a/b
a ^ b exponential: a raised to the b power
s(x) sine of x, x in radians
c(x) cosine of x, x in radians
a(x) actangent of x, returns radians
l(x) natural log of x
e(x) exponential log of raising e to the x
j(n, x) bessel function of integer order n of x
scale N show N fractional digits(default = 2)
calc> 54354 ^ 3
160581137553864
calc> quit