シェルでもC言語のようなif文が使用できる。
シェルで使用するif文の形式は次の通り。
if(条件) then
コマンド
endif
if(条件) then
コマンド1
else
コマンド2
endif
if(条件1) then
コマンド1
else if(条件2) then
コマンド2
.
.
else
コマンドn
endif
if(条件) コマンド
以上のようにthen と endif がC言語の { と } に対応すると思えば、大体C言語と似ている。
# if($x > 3 && $y < 5) echo test <- $xが3以上、$yが5以下のときコマンドを実行
# if(! -f test.ma) echo not exist <- test.maが存在しないときコマンドを実行
# if(1) echo test <- 条件が1(0以外)ならば test <- コマンド(echo)が実行される # if(0) echo test <- 条件が0ならばコマンド(echo)は実行されない
まず、コマンドラインからifを色々と試してみる。
ここでは実行するコマンドの例としてechoコマンドを使用しているが、もちろんこのコマンド以外のコマンドも使用できる。
# set x = 3 # if($x > 1) echo test <- 条件が成り立っているので test <- コマンド(echo)が実行される # if($x + 2 > 1) echo test <- 条件に数式を使う test # set y = 2.5 # if($y > 1.0) echo test <- 条件に実数が使われていると if: Badly formed number. <- エラーになる # set z = aaa # if($z == "aaa") echo test <- 文字列同士を比べる test # ls test1.ma test2.ma # set f = test1.ma # if(-f $f) echo $f exist <- test1.maが存在するかどうか調べてコマンドを実行 test1.ma exist # set files = ( `ls *.mb` ) # if($#files > 0) echo test <- *.mbファイルが1つ以上あればコマンドを実行 # cat test1.ma | wc -l <- test1.maの行数を調べるコマンド 238 <- test1.maの行数は238 # if(`cat test1.ma | wc -l` > 200) echo test <- test1.maの行数が200以上であればコマンドを実行 test
次に、ifを使ってtest.maというデータファイルが存在していればMAYAでレンダリングするシェルスクリプトを作って実行してみよう。
set f = test.ma
if(-f $f) then
Render -rd . $f
endif
# ls render.sh test.ma # csh render.sh ....... <- test.maをレンダリング # ls render.sh persp_test.iff test.ma #