0
0

More than 1 year has passed since last update.

ShellScriptの練習

Posted at

はじめに

シェルスクリプトの練習として以下のような処理を書いてみました。

・第1引数に性別、第2引数に年齢を入れる
・男性と女性について、20歳未満、男性20歳以上60歳未満、60歳以上で出力する文字を変える。(6区分)
・第1引数、第2引数に適切な値が入力されていなかった場合、エラーを返す。

実装

sample.sh
# 引数が2つなかったらエラーを返す
# -neはnot equall
if [ $# -ne 2 ];
then
# exitと1の間にスペース必要、最後はセミコロンを忘れない
  exit 1;
fi

# $1(第1引数)には性別、$2(第2引数)には年齢を入れる
# 年齢が負の数の場合エラー出力
if [ $2 -lt 0 ];
then
  exit 1;
fi

# 男性の年齢区分
if [ $1 = "man" ];
then
  if [ $2 -le 20 ];
    echo "Man:Child"
  elif [ $2 -le 60 ];
    echo "Man:Adult"
  else
    echo "Man:Elderly"
fi

# 女性の年齢区分
if [ $1 = "woman"];
then
  if [ $2 -le 20 ];
    echo "Woman:Child"
  elif [ $2 -le 60 ];
    echo "Woman:Adult"
  else
    echo "Woman:Elderly"
fi

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0