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?

More than 1 year has passed since last update.

シェルスクリプト ~基本・繰り返し&条件分岐~

Posted at

簡単なシェルスクリプト

1~100の数値のうち偶数の数値をechoさせる
簡単なことだけど色々詰まってるよね。

ステップ1 1~100の数値を出力させる。

 while文を使用し1~100まで出力させる。

#!/bin/bash
while [ $i -le 100 ]
  do
echo $i
i=$((i+1))
done 

 

ステップ2

  • if分を使い2で割り切れるときという条件を付ける

    -eqは演算子で左辺と右辺が等しい時ということ。

if [ $(( i % 2))  -eq 0 ]
then
	echo $1
fi

whilie文とかは普通にdo doneなのに、if文の終了がfiというのは?
とおもったけどfinishの省略か…

ステップ3

 二つを組み合わせる

#!/bin/bash

i=1
while [ $i -le 100 ]
do
	if [ $((i % 2)) -eq 0 ]
	then
		echo $i
	fi
	i=$((i+1))
done
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?