Shell-Script

將月份編號轉換為月份名稱

  • April 25, 2014

有沒有辦法將月份數字轉換為名稱?

例子:

2013-10-22 將變為 10 月 22 日

我沒有 GNU 日期,我的作業系統是 AIX。

如果您只需要將幾個鍵映射到值,只需使用數組

#!/bin/ksh

## cmdline argument is e.g. "2003-10-22"
DATE=$1

### extract day, month and year into separate variables
MONTHDAY=${DATE#*-}

YEAR=${DATE%%-*}
MONTH=${MONTHDAY%%-*}
DAY=${MONTHDAY#*-}

# an array to look up th month-names
# since month-numbers start with 1, the first element in the array is invalid.
set -A monthnames invalid Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

## perform the lookup
MONTHNAME=${monthnames[${MONTH}]}

## display "<Month> <DAY>"
echo ${MONTHNAME} ${DAY}

引用自:https://unix.stackexchange.com/questions/98296