LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] help compare date (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-help-compare-date-4175425371/)

LadyE 09-03-2012 03:12 AM

[bash] help compare date
 
Hi!
I have a problem with a bash script.
My script reads in input a date and a hour; I compare the format of it with regular expression for a completed script. But I could not solve this problem. My code is:
Code:

echo "Please insert date [dd/mm/yyyy]: "; read date
while [ ! ' echo " $date " | grep -E "[0-9]d{2}/[0-9]d{2}/[0-9]d{4}"' ];
        do
                echo "Please insert date [dd/mm/yyyy]: "; read date
        done
        echo "Please insert hour: [hh:mm] "; read hour
        while [ ! ' echo " $hout " | grep -E " ^(([0-1]?[0-9])|(2[0-3]))(:)[0-5]?[0-9]$ " ' ]; do
                echo "Please insert hour: [hh:mm] "; read hour
        done

I don't understand what is the error...
Also I put result of a grep in an array, is it possible?
Can you help me please?

pan64 09-03-2012 03:20 AM

probably hour is mistyped:
while [ ! ' echo " $hout " | grep -E " ^(([0-1]?[0-9])|(2[0-3]))(:)[0-5]?[0-9]$ " ' ];

LadyE 09-03-2012 03:34 AM

yes, it is "houR"...but my problems doesn't solved :(

ntubski 09-03-2012 05:38 AM

You shouldn't be using [ ] there, that is the test builtin command which only checks if the string is not empty:

Code:

while ! echo "$date" | grep -E '[0-9]d{2}/[0-9]d{2}/[0-9]d{4}' ;
#or
until echo "$date" | grep -E '[0-9]d{2}/[0-9]d{2}/[0-9]d{4}' ;


pan64 09-03-2012 05:55 AM

probably I do not understand, but what is that d in the regexp:
while ! echo "$date" | grep -E '[0-9]d{2}/[0-9]d{2}/[0-9]d{4}' ;
I think they should be removed

David the H. 09-04-2012 02:34 AM

I recommend a slightly different approach:

Code:

#!/bin/bash

reday='^([0-2][1-9]|[1-3]0|31)$'
remonth='^([0][1-9]|1[0-2])$'
reyear='^(19[0-9]{2}|20[0-9]{2})$'

while true; do

        IFS='/' read -r -p "Please input date (dd/mm/yyyy): " -a date

        if [[ ! ${date[0]} =~ $reday || ! ${date[1]} =~ $remonth || ! ${date[2]} =~ $reyear ]]; then

                echo "Improper date format detected.  Please try again." ; continue

        else
               
                printf '%s/%s/%s\n' "${date[@]}"
                break

        fi

done

I made some assumptions about narrowing down the date regexes, limiting it to 1999-2099. This can be easily modified. It also makes the false assumption that all months have 31 days. Testing for this would require quite a bit more work.

Follow this up with a similar loop for the hours.

LadyE 09-05-2012 03:56 AM

Thank you so much!!!
I have other problems...
I insert this information in a file and I research with a grep an occourrence.
The result of grep is composed from 4 elements and I need to printed 4° elements, even if there are 3 (in example).
I tried:
Code:

array=( $(grep "$occ" .eventi.txt) )
but this istruction inserts a row in the first index.
I created a background process with the fuction that does it...
How I kill this child process, before exiting the program? I don't know the PID...

Can you help me, please?:(

David the H. 09-07-2012 12:41 AM

We really need to see the input data, at a minimum, if you want proper help from us.

Please give us an example of the contents of the file, and point out what you need from it, in detail. Also post the function code you wrote, if possible. And what does the $occ variable contain?

At the very least, this...

Code:

array=( $(grep "$occ" .eventi.txt) )
...breaks the output of grep into one array element per whitespace-delimited* "word". If you quote the whole "$()", then the entire output will be treated as a single entry.

(*Unless you've reset the IFS environmental variable in some way, then it will split on whatever characters it contains.)

Safe processing would probably require another read loop or similar. But again we need to see what the input looks like.

LadyE 09-07-2012 02:12 AM

I'm sorry! you're right!
My script reads the following input data: name, date, hour and a message. With this informations it creates an event and my file contains a list of these events.
For example
Code:

name-event1 09/05/2000 15:40 message1
name-event2 04/04/2004 04:04 message2

My background process should give me a warning of an event (or more events) occur.
I put in $occ current date and current hour for "grep".
So, the output is "name-events date hour message"
But I have to print only the message of occurring event.
For example the 04/04/2004 at 04:04 my script print "message1".

I had thought to put my grep result into an array and after to print only 4th element...but i have problems if multiple events occur!
Also...to close the script I must kill background process. to kill process I need its PID. I can use "ps" command and copying the ouput in a file, so I can use another "grep" that give me only PID...I'm in a bind...:banghead:

bigearsbilly 09-07-2012 02:38 AM

You should explain what you are trying to do, not how to do it.
(I tell this to my boss all the time)

I would say you may be making life hard for yourself.

LadyE 09-07-2012 02:49 AM

I tried again ...
My script like as a memo.
if I want to remember a meeting between 5 minutes, I'll have a background process that between 5 minutes print "you have a meeting!"
When I close the script I must kill the background process


All times are GMT -5. The time now is 05:38 AM.