LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-13-2015, 10:50 AM   #1
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Rep: Reputation: Disabled
how to check a valid time argument in Linux?


Hello
I am writing a bash script xxx, which need to pass an time argument while using .
for example: xxx 4:30 ( 4:30 is 4:30 am )

I need to check in the script if the entered time is valid.
if it is not valid , I will echo " invalid input " something.


anyone could help?
thanks in advance
Ray
 
Old 08-13-2015, 11:46 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 770
Blog Entries: 3

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
A quick one...

Code:
$ echo "4:30" | grep '^[0-9][1-2]\{0,1\}:[0-5][0-9]'
4:30
Code:
#!/bin/bash

# Check if argument is a correct time

if [[ $(echo $1 | grep '^[0-9][1-2]\{0,1\}:[0-5][0-9]') ]]; then
    echo "Correct time"
    exit 0
else
    echo "Invalid time"
    exit 1
fi
On execution:
Code:
$ ./check_time.sh 9:12
Correct time
Code:
$ ./check_time.sh 4:71
Invalid time
Code:
$ ./check_time.sh 12:15
Correct time
This is a very simple regex that can be improved a lot. But since you are apparently dealing with am/pm times, and not a 24 hour clock, it is at least a start.

Best regards,
HMW
 
Old 08-13-2015, 03:13 PM   #3
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks.it is useful

Ray
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to check the argument value with a file Mitiraj Linux - Newbie 7 08-12-2014 02:05 AM
valid check of IP in C++ code Anandkpda Linux - Software 3 09-28-2011 02:20 PM
[SOLVED] Bash function check for valid IP jchambers Programming 4 04-17-2010 12:06 AM
how to check if a socket is valid in C ? naihe2010 Programming 3 10-29-2006 07:56 PM
Check if an int value is within valid range of enum? mic Programming 6 02-21-2006 12:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:39 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration