首页 > 系统服务 > 详细

shell脚本截取日期处理

时间:2015-05-15 10:45:36      阅读:131      评论:0      收藏:0      [点我收藏+]

[Author]: kwu 

在linux中日期格式化固定为8位:2015-01-01

如实现 2015-1-1 需要进行字符串处理


#!/bin/sh
# upload logs to hdfs


yesterday2="2015-01-08"

echo ${yesterday2}

first=${yesterday2:5:1}
second=${yesterday2:8:1}

echo ${first}
echo ${second}

res=$yesterday2
echo ${res}
  
if [ $first -eq 0 ] && [ $second -eq 0 ]
then
    res=${yesterday2:0:5}${yesterday2:6}
    res=${res:0:7}${res:8}
elif [ $first -eq 0 ]
then
    res=${yesterday2:0:5}${yesterday2:6}
elif [ $second -eq 0 ]
then
    res=${yesterday2:0:8}${yesterday2:9}
fi

echo ${res}

如上命令最终输出为

2015-1-8

shell脚本截取日期处理

原文:http://blog.csdn.net/bdchome/article/details/45740365

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!