濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > linux shell實(shí)現(xiàn)轉(zhuǎn)換輸入日期的格式

linux shell實(shí)現(xiàn)轉(zhuǎn)換輸入日期的格式

熱門(mén)標(biāo)簽:百度地圖標(biāo)注直線距離 德陽(yáng)400電話申請(qǐng) 鶴崗400電話申請(qǐng) 天津電話外呼系統(tǒng)排名 測(cè)繪地圖標(biāo)注名稱 怎么在百度地圖標(biāo)注公司的位置 智能電銷機(jī)器人有用嗎 外呼電話系統(tǒng)怎么操作 商機(jī)地圖標(biāo)注

對(duì)于用戶輸入日期的合法性檢驗(yàn),是個(gè)很重要的問(wèn)題,這個(gè)例子是簡(jiǎn)單得取得用戶輸入的日期,并轉(zhuǎn)換為相應(yīng)的格式,但不完美,原因請(qǐng)看后文。

#!/bin/sh
# normdate -- Normalizes month field in date specification
# to three letters, first letter capitalized. A helper
# function for Script #7, valid-date. Exits w/ zero if no error.

monthnoToName()
{
 # Sets the variable 'month' to the appropriate value
 case $1 in
  1 ) month="Jan"  ;; 2 ) month="Feb"  ;;
  3 ) month="Mar"  ;; 4 ) month="Apr"  ;;
  5 ) month="May"  ;; 6 ) month="Jun"  ;;
  7 ) month="Jul"  ;; 8 ) month="Aug"  ;;
  9 ) month="Sep"  ;; 10) month="Oct"  ;;
  11) month="Nov"  ;; 12) month="Dec"  ;;
  * ) echo "$0: Unknown numeric month value $1" >2; exit 1
  esac
  return 0
}

## Begin main script

if [ $# -ne 3 ] ; then
 echo "Usage: $0 month day year" >2
 echo "Typical input formats are August 3 1962 and 8 3 2002" >2
 exit 1
fi

if [ $3 -lt 99 ] ; then
 echo "$0: expected four-digit year value." >2; exit 1
fi

if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; then
 monthnoToName $1
else
 # Normalize to first three letters, first upper, rest lowercase
 month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')"
 month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')"
fi

echo $month $2 $3

exit 0

腳本分析:
1) 定義了函數(shù)monthnoToName(),用來(lái)轉(zhuǎn)換用戶輸入的數(shù)字月份
2)首先判斷參數(shù)的個(gè)數(shù)是否為3個(gè),其次判斷年份,接著是轉(zhuǎn)換月份格式。
3)if [ -z $(echo $1|sed 's/[[:digit:]]//g') ]; 這句話有點(diǎn)意思,是如果$1被執(zhí)行sed替換的話,即$1中存在數(shù)字
則執(zhí)行函數(shù)monthnoToName(),來(lái)轉(zhuǎn)換數(shù)字月份。
4)month="$(echo $1|cut -c1|tr '[:lower:]' '[:upper:]')"
month="$month$(echo $1|cut -c2-3 | tr '[:upper:]' '[:lower:]')"
將輸入的字符月份轉(zhuǎn)換為標(biāo)準(zhǔn)格式。
5)這個(gè)腳本最大的缺陷是雖然將日期的格式轉(zhuǎn)換了,但不能檢測(cè)過(guò)濾不存在的日期。

您可能感興趣的文章:
  • PowerShell中使用Get-Date獲取日期時(shí)間并格式化輸出的例子
  • Shell腳本遍歷一個(gè)日期范圍實(shí)例
  • perl與shell獲取昨天、明天或多天前的日期的代碼
  • 獲取兩個(gè)日期間隔時(shí)間的shell腳本代碼
  • PowerShell中iso8601格式日期和DateTime對(duì)象互轉(zhuǎn)實(shí)例
  • 一個(gè)shell小案例(創(chuàng)建日期目錄)
  • 判斷輸入的日期是否正確的shell腳本
  • linux shell中 if else以及大于、小于、等于邏輯表達(dá)式介紹
  • 一個(gè)不錯(cuò)的shell 腳本教程 入門(mén)級(jí)
  • 利用shell獲取指定日期前N天的日期

標(biāo)簽:武漢 自貢 鎮(zhèn)江 丹東 滁州 優(yōu)質(zhì)小號(hào) 六盤(pán)水 百色

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《linux shell實(shí)現(xiàn)轉(zhuǎn)換輸入日期的格式》,本文關(guān)鍵詞  linux,shell,實(shí)現(xiàn),轉(zhuǎn)換,輸入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《linux shell實(shí)現(xiàn)轉(zhuǎn)換輸入日期的格式》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于linux shell實(shí)現(xiàn)轉(zhuǎn)換輸入日期的格式的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    昌吉市| 榆中县| 曲松县| 柳林县| 陵川县| 新绛县| 洪泽县| 佳木斯市| 唐河县| 邢台县| 惠州市| 穆棱市| 阜城县| 高密市| 延吉市| 海丰县| 威宁| 齐齐哈尔市| 城市| 射阳县| 博兴县| 阳东县| 德令哈市| 钟祥市| 登封市| 新密市| 全椒县| 临海市| 大安市| 钟祥市| 盖州市| 牡丹江市| 四平市| 垦利县| 临安市| 昌黎县| 榕江县| 安徽省| 阿坝| 黎川县| 沐川县|