[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