LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 09: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, 10:46 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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, 02:13 PM   #3
RuijunFan
LQ Newbie
 
Registered: Oct 2014
Posts: 13

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

Ray
 
  


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

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

All times are GMT -5. The time now is 05:04 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
Open Source Consulting | Domain Registration