bash-cases中的日期格式
我需要接收bash中的代码,该代码将以以下格式显示当前日期:bash-cases中的日期格式,bash,date,Bash,Date,我需要接收bash中的代码,该代码将以以下格式显示当前日期: 2016年1月1日星期日(上标st,nd,rd或th)4例 收到上标的方式是什么 我将感谢您的帮助。我希望这有助于: #!/bin/bash # get the day of the month to decide which postfix should be used dayOfMonth=$(date +%d) # Choose postfix case "$dayOfMonth" in 1) postfix
2016年1月1日星期日
(上标st
,nd
,rd
或th
)4例
收到上标的方式是什么
我将感谢您的帮助。我希望这有助于:
#!/bin/bash
# get the day of the month to decide which postfix should be used
dayOfMonth=$(date +%d)
# Choose postfix
case "$dayOfMonth" in
1)
postfix="st"
;;
2)
postfix="nd"
;;
3)
postfix="rd"
;;
*)
postfix="th"
;;
esac
# Generate date string
myDate=$(date +%A,\%d\^$postfix\ %B\ %Y)
echo $myDate
说明:
#!/bin/bash
# Generate date template
dateTpl=$(date +%A,\ \%d\^postfix\ %B\ %Y)
# get the day of the month from the template, to decide which postfix should be use
dayOfMonth=$(echo $dateTpl | cut -d ' ' -f 2 | sed 's/\^postfix//g' )
# Choose postfix
case "$dayOfMonth" in
1)
postfix="st"
;;
2)
postfix="nd"
;;
3)
postfix="rd"
;;
*)
postfix="th"
;;
esac
# Generate date string from template
myDate=$(echo $dateTpl | sed "s/postfix/$postfix/g")
echo $myDate
date
程序没有任何生成序号的转换,因此您需要替换其外部的后缀:
#!/bin/bash
d=$(date +%e)
case $d in
1?) d=${d}th ;;
*1) d=${d}st ;;
*2) d=${d}nd ;;
*3) d=${d}rd ;;
*) d=${d}th ;;
esac
date "+%A, $d %B %Y"
请注意,大多数英语风格指南建议不要将顺序后缀写为上标;如果你真的坚持,你可以把它当作一种锻炼
还要注意的是,通过调用
date
两次,我们可能会在午夜出现竞争状况。我们可以通过单独查找当前时间,然后对其进行格式化来避免这种情况:
#!/bin/bash
s=$(date +@%s)
d=$(date -d $s +%e)
case $d in
1?) d=${d}th ;;
*1) d=${d}st ;;
*2) d=${d}nd ;;
*3) d=${d}rd ;;
*) d=${d}th ;;
esac
date -d $s "+%A, $d %B %Y"
或者,将日期(一次)读入单独的变量,然后在shell中格式化:
#!/bin/bash
IFS=_ read a d b y < <(date +%A_%e_%B_%Y)
case $d in
1?) d=${d}th ;;
*1) d=${d}st ;;
*2) d=${d}nd ;;
*3) d=${d}rd ;;
*) d=${d}th ;;
esac
echo "$a, $d $b $y"
#/bin/bash
IFS=uu阅读a d b y