Shell 从两个日期时间之间的差异获取另一个时间

Shell 从两个日期时间之间的差异获取另一个时间,shell,awk,sed,Shell,Awk,Sed,我有以下资料: 秒(s)、分钟(m)、小时(h)和天(d),如中所述 下面:并希望通过从当前日期时间减去给定时间将其转换为日期和时间。 DATE1-DATE2=DIFF,希望通过给出DATE1和DIFF来计算DATE2 Means if current time is 2016-04-02T12:00:00 and given input is 23H60M then output should be 2016-04-01T12:00:00, and it can be 23D24H,can b

我有以下资料: 秒(s)、分钟(m)、小时(h)和天(d),如中所述 下面:并希望通过从当前日期时间减去给定时间将其转换为日期和时间。 DATE1-DATE2=DIFF,希望通过给出DATE1和DIFF来计算DATE2

Means if current time is 2016-04-02T12:00:00 and given input is 23H60M then output should be 2016-04-01T12:00:00, and it can be 23D24H,can be 23M20S...
400d - Only days (d) displayed

20d23h - days (d) and hours (h) displayed

14h33m - hours (h) and minutes (m) displayed

10m59s - minutes (m) and seconds (s) displayed

你可以试试这样的东西

#!/bin/bash
input=$(echo $1 | tr '[:lower:]' '[:upper:]')

if [[ $input == *"D"* ]]
then
input=$(echo $input | sed 's/D/ days ago /g')
fi

if [[ $input == *"H"* ]]
then
input=$(echo $input | sed 's/H/ hours ago /g')
fi

if [[ $input == *"M"* ]]
then
input=$(echo $input | sed 's/M/ minutes ago /g')
fi

if [[ $input == *"S"* ]]
then
input=$(echo $input | sed 's/S/ seconds ago /g')
fi
echo $input


CURRENTDATE="$(date +%Y-%m-%d-%H:%M:%S)"
echo $CURRENTDATE
OLDERDATE="$(date "+%Y-%m-%d-%H:%M:%S" -d "$input")"
echo $OLDERDATE

这不是一个代码编写服务。尤其是当你的问题看起来像是家庭作业的时候。