shellscript tips
2008年08月08日 (shellscript)
- 変数命名規則
- カウント変数などの一時的な変数は全て小文字,非一時的な変数は全て大文字にする.
- ループ処理
- 簡単な処理を2,3回程しか実行しないループ処理は実効速度の低下,可読性の悪化などを引き起こすので避ける.
- 無駄な出力
- 他のコマンドにパイプ,リダイレクトされることも考慮にいれて無駄な出力はさせないようにする.
- エラー処理
- ユーザの誤った入力などに対しコマンドの用法などを示すなどしてからプログラムを終了させる.例外処理を行う.
- コメント
- 適時コメントを入れるなどしてその関数が何をする関数なのかなどを明確にしめす.
PR
sample_while.sh
2008年08月04日 (shellscript)
source
#!/bin/sh
#sample while
i=0
while [ $i -lt 10 ]
do
echo $i
i=`expr $i + 1`
done
sample_until.sh
2008年08月04日 (shellscript)
source
#!/bin/sh
#sample until
i=0
until [ $i -gt 10 ]
do
echo $i
i=`expr $i + 1`
done
sample_read.sh
2008年08月04日 (shellscript)
source
#!/bin/sh #sample read echo -n "input : " read MYIN echo $MYIN
sample_if.sh
2008年08月04日 (shellscript)
source
#!/bin/sh
#sample if
if [ $1 -eq 5 ]; then
echo "$1 = 5"
elif [ $1 -lt 5 ]; then
echo "$1 < 5"
elif [ $1 -gt 5 ]; then
echo "$1 > 5"
fi