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?

【Shell Script】カレンダーを自作してみる

Posted at

はじめに

calコマンドのカレンダーを自作すると力がつくとのことだったのでやってみた

コード

#!/bin/bash

year=$1
month=$2

# 0=日曜 6=土曜
firstWeekDay=$(date -d "${year}-${month}-01" +%w)
echo "      ${year}${month}月"
echo "Sa Mo Tu We Th Fr Sa"

# 月末
numDay=$(date -d"${year}-${month}-01 1 month 1 day ago" +%d)

# 初日の位置
for((i=1;i<=${firstWeekDay};i++))
do
    echo -n "   "
done

for((i=1;i<=numDay;i++))
do
    # 曜日番号を取得
    weekDay=$(date -d "${year}-${month}-${i}" +%w)
    # 土曜で改行
    if [ ${weekDay} -eq 6 ]; then
        if [ ${i} -gt 9 ]; then
            echo "${i} "
        else
            echo " ${i} "
        fi

    elif [ ${i} -gt 9 ]; then
        echo -n "${i} "

    else
        echo -n " ${i} "
    fi
done

所感

今回初めて触ることになったbash。構文が普段触っている言語に比べわかりづらい。特にif文
if [ ... ]
スペースを入れないと動かないのを知らなかったので色々調べる羽目になってもうた。

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?