Date not correct so it changed to current date.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date not correct so it changed to current date.
# 1  
Old 12-29-2017
Date not correct so it changed to current date.

Hello, so basically i have a txt file containing "foto's" named YYYY-MM-DD_HH.mm.ss.jpeg.
But since it can probably not convert it it changes the date to the current date. What am i doing wrong?

Code:
#!/bin/bash

inputDateFmt()
{
   sed -e 's/_/ /g' -e 's/\./:/g' <<< "$1"
}
begindatum=$(date -u -d "$(inputDateFmt "2014-10-27_08.01.01")")
einddatum=$(date -u -d "$(inputDateFmt "2014-10-27_18.00.01")")

while [ "$begindatum" != "$einddatum" ]
do
    bestandnaam=$(date --date="$begindatum" +"%Y-%m-%d_%H.%M.%S.jpeg")
    if [ $(grep -Fx "$bestandnaam" fotos.txt) ]
    then
        echo "$bestandnaam found"
    else
       echo  "$bestandnaam not found"
    fi
    begindatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$begindatum"+1minute)

done

This is the error i'm getting (basically it is saying that it is a invalid date)
Date not correct so it changed to current date.-pqgaplfdqi_y4e8eh8jdsapng
# 2  
Old 12-29-2017
It looks ok, except LC_ALL=C date --utc --iso-8601=seconds -d"$begindatum"+1minute returns something that will never match einddatum (at least, on my machine).

Run it with bash -x script.sh and check what the date commands are actually doing.
# 3  
Old 12-29-2017
Quote:
Originally Posted by CarloM
It looks ok, except LC_ALL=C date --utc --iso-8601=seconds -d"$begindatum"+1minute returns something that will never match einddatum (at least, on my machine).

Run it with bash -x script.sh and check what the date commands are actually doing.
So i gave it a fix by adding
Code:
einddatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$einddatum")

I checked this first on an online bash shell tester and it works fine (just to see if the time goes up and if it stops if it matches)
Code:
inputDateFmt()
{
   sed -e 's/_/ /g' -e 's/\./:/g' <<< "$1"
}
begindatum=$(date -u -d "$(inputDateFmt "2014-10-27_08.01.01")")
einddatum=$(date -u -d "$(inputDateFmt "2014-10-27_18.00.01")")

echo "$begindatum"
echo "$einddatum"

while [ "$begindatum" != "$einddatum" ]
do

begindatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$begindatum"+1minute)
einddatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$einddatum")

echo "$begindatum"

done

So i tried this then in my code
Code:
#!/bin/bash

inputDateFmt()
{
   sed -e 's/_/ /g' -e 's/\./:/g' <<< "$1"
}
begindatum=$(date -u -d "$(inputDateFmt "2014-10-27_08.01.01")")
einddatum=$(date -u -d "$(inputDateFmt "2014-10-27_18.00.01")")

while [ "$begindatum" != "$einddatum" ]
do
    bestandnaam=$(date --date="$begindatum" +"%Y-%m-%d_%H.%M.%S.jpeg")
    if [ $(grep -Fx "$bestandnaam" fotos.txt) ]
    then
        echo "$bestandnaam found"
    else
       echo  "$bestandnaam not found"
    fi
    begindatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$begindatum"+1minute)
    einddatum=$(LC_ALL=C date --utc --iso-8601=seconds -d"$einddatum")

done

but this resulted in the error: (check image below)
Date not correct so it changed to current date.-gyurfhu7qbsucf77png
# 4  
Old 12-29-2017
Not sure I fully understand what you're doing and what you're after, but a few comments:

Why do you use (convert to) date formats like Mon Oct 27 18:00:01 CET 2014 when the date/time part of your file names is YYYY-MM-DD_HH.mm.ss, i.e. totally different?

info date:
Quote:
The output of the ‘date’ command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’. When using ‘date’ to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.
Why do you convert "_" chars to " " and "." to ":" if you dont use those afterwards?

Why do you specify the time stamps down to the second but iterate the loop in one minute steps only?

Would it be feasible to do all the looping, calculating etc. on seconds since epoque, and then convert those to a time string for the comparison / grepping? Would it make sense to forgo the seconds when grepping?

And, in lieu of the sed invocation in inputDateFmt, did you consider bash's "Parameter Expansion: Pattern substitution"?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

4. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

5. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

6. Shell Programming and Scripting

Fetch date of 7 years back from current date in Perl

$beginDate = substr(DateCalc("today", "-7Days"),0,8); This fetches the date 7 days back Can I fetch the date before 7 years from todays date in Perl using same syntax Use code tags, see PM. (3 Replies)
Discussion started by: parthmittal2007
3 Replies

7. Shell Programming and Scripting

csv file field needs to be changed current system date with awk

HI, I have csv file with records as shown below. 4102,Bangalore,G10,21,08/17/2011 09:28:33:188,99,08/17/2011 09:27:33:881,08/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

8. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

9. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question