shell脚本截取日期处理

来源:互联网 发布:网络污词大全 编辑:程序博客网 时间:2024/05/01 15:41

[Author]: kwu 

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

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


#!/bin/sh# upload logs to hdfsyesterday2="2015-01-08"echo ${yesterday2}first=${yesterday2:5:1}second=${yesterday2:8:1}echo ${first}echo ${second}res=$yesterday2echo ${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}fiecho ${res}

如上命令最终输出为

2015-1-8

1 0
原创粉丝点击