LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2012, 03:12 AM   #1
LadyE
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Rep: Reputation: Disabled
[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?
 
Old 09-03-2012, 03:20 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,903

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
probably hour is mistyped:
while [ ! ' echo " $hout " | grep -E " ^(([0-1]?[0-9])|(2[0-3]))(:)[0-5]?[0-9]$ " ' ];
 
Old 09-03-2012, 03:34 AM   #3
LadyE
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
yes, it is "houR"...but my problems doesn't solved
 
Old 09-03-2012, 05:38 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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}' ;
 
Old 09-03-2012, 05:55 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,903

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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
 
Old 09-04-2012, 02:34 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 09-05-2012, 03:56 AM   #7
LadyE
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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?

Last edited by LadyE; 09-05-2012 at 07:18 AM.
 
Old 09-07-2012, 12:41 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 09-07-2012, 02:12 AM   #9
LadyE
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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...
 
Old 09-07-2012, 02:38 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 09-07-2012, 02:49 AM   #11
LadyE
LQ Newbie
 
Registered: Sep 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH ... Date /days subtraction Addition and compare em31amit Programming 5 05-24-2012 01:12 PM
Date comparison with 'string date having slashes and time zone' in Bash only TariqYousaf Programming 2 10-08-2009 07:37 AM
how to compare date string?? loplayers Linux - Newbie 1 11-29-2007 08:40 PM
compare date uusing shell programming please... izza_azhar Programming 7 01-14-2005 07:24 AM
php date compare omarswan Programming 2 10-02-2002 04:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration