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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-03-2012, 03:12 AM
|
#1
|
|
LQ Newbie
Registered: Sep 2012
Posts: 5
Rep: 
|
[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?
|
|
|
|
09-03-2012, 03:20 AM
|
#2
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,745
|
probably hour is mistyped:
while [ ! ' echo " $hout " | grep -E " ^(([0-1]?[0-9])|(2[0-3]))(:)[0-5]?[0-9]$ " ' ];
|
|
|
|
09-03-2012, 03:34 AM
|
#3
|
|
LQ Newbie
Registered: Sep 2012
Posts: 5
Original Poster
Rep: 
|
yes, it is "houR"...but my problems doesn't solved 
|
|
|
|
09-03-2012, 05:38 AM
|
#4
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,015
|
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}' ;
|
|
|
|
09-03-2012, 05:55 AM
|
#5
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,745
|
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
|
|
|
|
09-04-2012, 02:34 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,558
|
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.
|
|
|
|
09-05-2012, 03:56 AM
|
#7
|
|
LQ Newbie
Registered: Sep 2012
Posts: 5
Original Poster
Rep: 
|
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.
|
|
|
|
09-07-2012, 12:41 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,558
|
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.
|
|
|
|
09-07-2012, 02:12 AM
|
#9
|
|
LQ Newbie
Registered: Sep 2012
Posts: 5
Original Poster
Rep: 
|
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... 
|
|
|
|
09-07-2012, 02:38 AM
|
#10
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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.
|
|
|
|
09-07-2012, 02:49 AM
|
#11
|
|
LQ Newbie
Registered: Sep 2012
Posts: 5
Original Poster
Rep: 
|
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
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|