Shell脚本——获取当前日期和时间

背景

  • 需要在脚本中获取当前系统日期,命名日志文件

操作

  • 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"

 

posted @ 2020-03-24 10:41  话·醉月  阅读(34872)  评论(0编辑  收藏  举报