0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者向け】しまさんのLinuxシェルクリプト ドリル if文編①(解説付き)

Posted at

問題①

第1引数が「10」なら「入力した数値は10」,「20以下」なら「入力した数値は20より小さい」、「60以上」なら「入力した数値は60以上」、それ以外は「20以上60未満」と表示させて下さい

回答
#!/bin/bash
num=$1

# 条件分岐
if [ "$num" -eq 10 ]; then
     echo "入力した数値は10"
elif [ "$num" -le 20 ]; then
echo "入力した数値は20より小さい"
elif [ "$num" -ge 60 ]; then
  echo "入力した数値は60以上"
else
  echo "入力した数値は20以上60未満"
fi

解説:
-eq, -le, -ge は整数比較用の演算子です([ "$num" -eq 10 ] など)

$1 はスクリプトに渡された第1引数を指します

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?