LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Trying to compare date in if satement (https://www.linuxquestions.org/questions/linux-general-1/trying-to-compare-date-in-if-satement-4175535106/)

dingo-den 02-25-2015 07:51 PM

Trying to compare date in if satement
 
I have a script that creates files and at midday UTC I would like to copy it to a web server. I can't seem to get the if statement correct. I don't know the exact time that I will have an image but it will be between midday and 30 seconds past midday. I tried this but it does not copy the files ?

Code:

#!/bin/bash
#

DATE2=$(date +"%H-%M-%S")

 if [[ $DATE2 -gt "12-00-00" && $DATE2 -lt "12-30-00" ]] ; then
        cp file;
        cp file;
        cp file;
fi


Any help would be appreciated.

suicidaleggroll 02-25-2015 07:57 PM

Get rid of the dashes and it'll work as-is.
Code:

DATE2=$(date +"%H%M%S")
if [[ "$DATE2" -gt "120000" && "$DATE2" -lt "123000" ]] ; then
...

That's a bit of a weird way to go about this though, if you describe your problem in more detail I'm sure somebody here can suggest a more elegant solution.

dingo-den 02-25-2015 08:34 PM

Quote:

Originally Posted by suicidaleggroll (Post 5323411)
Get rid of the dashes and it'll work as-is.
Code:

DATE2=$(date +"%H%M%S")
if [[ "$DATE2" -gt "120000" && "$DATE2" -lt "123000" ]] ; then
...

That's a bit of a weird way to go about this though, if you describe your problem in more detail I'm sure somebody here can suggest a more elegant solution.

I tried your solution and it did not work I got this error. Must be something I am doing.
Quote:

line 157: [[: 023209: value too great for base (error token is "023209")
This is what I have in the code. The time is different as I am trying to get it to work now.

Code:

DATE2=$(date +"%H%M%S")
if [[ "$DATE2" -gt "023200" && "$DATE2" -lt "023230" ]] ; then

        cp CamImglo.jpg /var/www/html/Jpeg/netcam-midday.jpg
        cp CamImg2.jpg /var/www/html/Jpeg/middaybig.jpg
        cp netcam2.jpg /var/www/html/Jpeg/netcam2-midday.jpg
fi


dingo-den 02-25-2015 08:47 PM

My problem is this.

I have a script that gets an image from a camera every 30 secs or there abouts. I do not know the exact time as it changes because of the script.
Quote:

#!/bin/bash
while true
do
/scripts/corona2webcam.sh &> /dev/null
sleep 28
done
I keep a copy of all the images and at midday I would like to put an image on my web page as Midday. Because I do not know what the name of the image will, be as they are numbered as they are created, so I can use avconv to create a movie at midnight. I need to find the file that is created at midday, the file looks like this 18705 Feb 26 02:25 img-249.jpg.

So that is why I trying to get the time as I do not know how to find the file that may be created between 000000 and 000030.

rknichols 02-25-2015 08:48 PM

Numbers with a leading zero will be interpreted as octal, and "9" is not a valid octal digit. Include an explicit base specification:
Code:

if [[ 10#"$DATE2" -gt 10#"023200" && 10#"$DATE2" -lt 10#"023230" ]] ; then
Still, never seen anyone trying to compare times that way. But, whatever works.

dingo-den 02-25-2015 09:16 PM

Quote:

Originally Posted by rknichols (Post 5323433)
Numbers with a leading zero will be interpreted as octal, and "9" is not a valid octal digit. Include an explicit base specification:
Code:

if [[ 10#"$DATE2" -gt 10#"023200" && 10#"$DATE2" -lt 10#"023230" ]] ; then
Still, never seen anyone trying to compare times that way. But, whatever works.


That worked just fine. I am not a programmer so my code is not very sophisticated but it works most of the time.


All times are GMT -5. The time now is 04:06 AM.