2
1

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 3 years have passed since last update.

3の倍数でahoになるシェルプログラム

Posted at

はじめに

Bashプログラミングの小ネタとして作成しました。
昔流行った?3の倍数や3が含まれる数でahoになります。

ソース

#!/bin/bash

echo -n "Please input end number => "
read num

count=1

while (( $count <= $num ))
do
  if (( $count % 3 == 0 ))
    then
      echo "aho"
    else if  [ "`echo $count | grep 3`" ]
      then
        echo "aho"
      else
        echo $count
    fi
  fi
  (( count+=1 ))
  sleep 1
done

実行結果

$ ./aho.sh

Please input end number => 15
1
2
aho
4
5
aho
7
8
aho
10
11
aho
aho
14
aho

おわりに

このプログラムを作成することで

  • 分岐構文
  • 繰り返し構文
  • testコマンド([]や``)
  • Bashのお作法

が理解出来ました。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?