编写shell脚本以用am或pm表示法显示时间 [英] Writing shells script to display time in am or pm notation
问题描述
我正在尝试编写一个shell脚本,该脚本以am或pm显示时间,而不是使用date命令显示时间的方式.我不确定该怎么做.我想对于初学者来说,如何从date命令中提取时间以进行操作?以及如何提取一个小时的数字来进行操作?
I'm trying to write a shell script that displays the time in am or pm rather than the way it is shown using date command. I'm not sure how I should go about doing this. I guess for starters, how do I extract the time from the date command to manipulate it? And how do I extract the number for the hour to manipulate it?
推荐答案
您正在寻找的是
man strftime
代表字符串格式时间,是 date
使用的格式.
That stands for string-format-time, which is the format that date
uses.
因此,要获取当前的AM/PM,请使用以下命令:
So to get the current AM/PM, use the following:
date +"%p"
或
ampm=`date +"%p"`
echo $ampm
小时可以是几种格式之一(您想要12或24小时制,是否领先零?)我猜您想要的是非领先零的12小时制:
The hour can be one of several formats (do you want 12 or 24 hour time, leading zero or not?) I'm guessing you want non-leading-zero 12 hour time:
date +"%l"
这篇关于编写shell脚本以用am或pm表示法显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!