7

Mac如何在shell脚本当中获取时间

 2 years ago
source link: https://ppmoon.github.io/posts/shell/mac-how-to-get-time-in-shell-script/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Mac如何在shell脚本当中获取时间 | PPmoon's Blog

Mac如何在shell脚本当中获取时间

Posted on 2019-08-26 in shell • 405 words • 1 minute read
Tags: shell

Mac如何在shell脚本当中获取时间

获取今天的时间

date
2019年 8月26日 星期一 14时02分23秒 CST

直接输入date就可以获取当前的时间(CST时间)

date -jnRu
Mon, 26 Aug 2019 06:06:29 +0000

如果加上参数-jnRu会显示格林威治时间(GMT)

时间格式化

date +"%Y-%m-%d %H:%M:%S"
2019-08-26 14:28:14

格式化数据可以直接使用加号,然后配合日期符号输出 常用日期符号

符号 代表意义
%Y
%m
%d
%H
%M
%S

获取昨天的时间

mac下的date命令计算时间的方法和linux会不太一样

命令参数为[-v[+|-]val[ymwdHMS]]

date 后加 -v 加减一个值,最后填写格式化参数格式化输出。都是在当前时间的基础上进行加减运算的

date -v-1d +"%Y-%m-%d"
2019-08-25
date -v-1y +"%Y-%m-%d"
2018-08-26
date -v+1m +"%Y-%m-%d"
2019-09-26
date -v+1H +"%Y-%m-%d %H:%M:%S"
2019-08-26 15:40:36
date -v+1M +"%Y-%m-%d %H:%M:%S"
2019-08-26 14:41:58
date -v+1S +"%Y-%m-%d %H:%M:%S"
2019-08-26 14:40:12

编写shell脚本执行的时候需要使用到时间,更多关于date命令的使用方法可以使用

man date

进行查看详细的使用方法或者输入一个错误的参数比如

date h
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

会提示提示部分使用方法。


Find me around the web:
GitHub

Copyright © 2021 PPmoon. This work is licensed under the CC BY-SA 4.0 license.

Built with Hugo, using the theme pure, which was influenced by the theme smigle.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK