玩转 Shell 脚本:轻松获取当前的日期与时间
最编程
2024-08-08 17:59:41
...
背景
- 需要在脚本中获取当前系统日期,命名日志文件
操作
- Linux的系统时间是可以直接调用系统变量获取,如下图所示
详解
- % Y 年(例如:1970,1996等)
- % y 年的最后两个数字( 1999则是99
- % m 月(01…12)
- % D 日期(mm/dd/yy)
- % d 一个月的第几天(01…31)
- % H 小时(00…23)
- % I 小时(01…12)
- % M 分(00…59)
- % S 秒(00…59)
例子
- 我的脚本如下
#!/bin/sh . /etc/profile echo "orderbiz is start" type="orderBiz" logpath="/home/jingguoliang/project/sh/" currentdate=$(date +%Y%m%d) logdatefile=$logpath"orderbiz"$currentdate".log" if [ -e $logdatefile ];then touch $logdatefile fi java -jar /home/jingguoliang/project/com.canaan.superviser.rpc-dev.jar $type >> $logdatefile & echo "orderbiz is end"
上一篇: 如何在本地获取当前时间?
推荐阅读
-
玩转 Shell 脚本:轻松获取当前的日期与时间
-
玩转Rust的时间功能:如何获取、计算和操作当前日期与时间?
-
玩转Java 8的LocalDateTime:毫秒和秒的时间戳获取,以及日期与字符串、LocalDateTime之间的转换
-
用PHP轻松获取当前的日期、时间和年月日
-
掌握Dart或Flutter编程:轻松获取当前日期的教程与示例
-
Java中获取当前日期和时间的多种方法:详解New Date与java.util.Date的区别
-
玩转Linux Shell:日期与时间的魔法
-
玩转电脑时空穿越:轻松手动调整日期与时间,实现现下过去或未来的显示效果——只需两步操作,让你的电脑自动同步时间变化
-
玩转Moment.js系列第三弹:轻松应对日期与时间的增删改操作
-
在Django中,如何轻松让DateTimeField默认设置为当前时间并允许后续编辑——玩转Django日期时间字段的方法