2009-10-17から1日間の記事一覧

浮動小数点の演算を行う

小数を含む数値の計算を行うにはbcコマンドが使われる #!/bin/sh #float.sh A=5 B=7 echo 'scale=5; sqrt(1/3)' | bc

文字列が整数かを調べる

testコマンドで数値と文字列を比較するとエラーになる 文字列が整数でなければ終了コードが2以上になる #!/bin/sh #isnum.sh is_num(){ [ "$1" -eq 0 ] 2>/dev/null if [ $? -ge 2 ]; then echo "$1 is NOT a valid integer." else echo "$1 is a valid inte…

乱数を得る(bash)

shには乱数を生成する機能がない bashのシェル変数RANOMは0-32767までの乱数を返す 乱数の範囲を1-Nまでにしたい場合は $ ( ( (RANDOM % n) + 1 ))のようにする #!/bin/sh #random_bash.sh for n in 1 2 3 4 5 6 7 8 9 10; do random=$(( (RANDOM % 10) + 1 …

数値の比較を行う

testコマンドを使う #!/bin/sh #int_compare.sh a=3 b=5 c=3 if [ $a -lt $b ]; then echo "$a is less then $b" fi if [ $a -eq $c ]; then echo "$a is equal to $c" fi if [ $a -eq $b ]; then echo "$a is equal to $b" else echo "$a is NOT equal to $…