Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2822

Bashで3パターンのおみくじを作った

$
0
0

はじめに

シェルスクリプトの学習として作りました。
作ったおみくじはifを使ったもの、caseを使ったもの、配列を使ったものの3パターンです。

ifを使ったおみくじ

#!/bin/bashnumber=$(($RANDOM%6))if[$number-eq 0 ];then
        echo 大吉
elif[$number-eq 1 ];then
        echo 中吉
elif[$number-eq 2 ];then
        echo 小吉
elif[$number-eq 3 ];then
        echo 末吉
elif[$number-eq 4 ];then
        echo else
        echo 大凶
fi

caseを使ったおみくじ

#!/bin/bashnumber=$(($RANDOM%6))case"$number"in
        0)echo 大吉
                ;;
        1)echo 中吉
                ;;
        2)echo 小吉
                ;;
        3)echo 末吉
                ;;
        4)echo ;;
        5)echo 大凶
                ;;esac

配列を使ったおみくじ

#!/bin/bashf=("大吉""中吉""小吉""末吉""凶""大凶")num=$(($RANDOM%6))echo${f[$num]}

以上です


Viewing all articles
Browse latest Browse all 2822

Latest Images

Trending Articles