ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
#!/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.
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...
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.
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
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...
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.