My task: At the entrance I get a date with the time in the form of "20170101 14:00:00". I need to take an hour and save to a variable for future use.
What I did: After a few hours of google and forums, I figured out some shell basics. I read in several articles about date. But unfortunately the script does not what I expect from it.
The script recognizes the date and formats the correct view. But very unpredictable behaves "-1hour". I get the correct date at "+ 3hours" and so far I have adopted it for myself in the script. Could any unexpected results occur in the future? Is it connected in some way with time zones?
Question: Why does “-” add time, and “+” take away? And why suddenly time starts to be calculated from "4:00:00" when you enter "2:00:00"? I would be very grateful for podskaskam or links where I can learn my knowledge in shell scripts.
DATE="20170101 14:00:00" dat2=$(date -d "$DATE" +'%Y%m%d %H%M%S') ECHO $dat2 ECHO date -d "$DATE +0hour" +'%Y%m%d %H%M%S' date -d "$DATE +1hour" +'%Y%m%d %H%M%S' date -d "$DATE +2hour" +'%Y%m%d %H%M%S' date -d "$DATE +3hour" +'%Y%m%d %H%M%S' date -d "$DATE +4hour" +'%Y%m%d %H%M%S' ECHO date -d "$DATE -1hour" +'%Y%m%d %H%M%S' date -d "$DATE -2hour" +'%Y%m%d %H%M%S' date -d "$DATE -3hour" +'%Y%m%d %H%M%S output:
20170101 140000 20170101 160000 20170101 150000 20170101 140000 20170101 130000 20170101 120000 20170101 170000 20170101 180000 20170101 190000