Check if time format is valid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if time format is valid
# 1  
Old 10-20-2015
Check if time format is valid

How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid.

Code:
echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }'

ex:
./script.ksh 12:34:21 = okay
./script.ksh 26:67:92 = invalid/exit script
# 2  
Old 10-20-2015
Hello erin00,

Could you please try following and let me know if this helps.
Code:
cat script.ksh
echo $1 | awk -F ':' '{match($0,/[0-2][0-4]:[0-5][0-9]:[0-5][0-9]/);A=substr($0,RSTART,RLENGTH);if(A  && $1 !~ /24/){ print "good"}  else {print "bad" }}'

After running script above following will be the output.
Code:
./script.ksh 12:34:21
good
./scrpit.ksh 26:67:92
bad
./script.ksh 24:17:12
bad

You could use string Invalid in place of bad which I have used above. Also on a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-20-2015 at 04:36 AM..
# 3  
Old 10-20-2015
This works the same as the code above, but how can I exit the script if the time is invalid
# 4  
Old 10-20-2015
Try this pure shell approach:
Code:
B=(${1//:/ })
[ ${B[0]} -le 23 -a ${B[1]} -le 59 -a ${B[2]} -le 59 ] && echo good || echo bad

---------- Post updated at 09:55 ---------- Previous update was at 09:54 ----------

Use exit instead of echo bad.
# 5  
Old 10-20-2015
this ain't working.
Code:
`(' is not expected.

# 6  
Old 10-20-2015
Quote:
Originally Posted by erin00
This works the same as the code above, but how can I exit the script if the time is invalid
Hello erin00,

Could you please try following and let me know if this helps.
Code:
cat script.ksh
echo $1 | awk -F ':' '{match($0,/[0-2][0-4]:[0-5][0-9]:[0-5][0-9]/);A=substr($0,RSTART,RLENGTH);if(A && $1 !~ /24/){ print "good";exit 0}  else {print "bad";exit 1;}}'
if [[ $? == 0 ]]
then
     echo "we passed GOOD time test"
else
     exit;
fi

When I run the script with different arguments to check it's functionality it give following output then.
Code:
./script.ksh 24:17:12
bad
./script.ksh 23:17:12
good
we passed GOOD time test

Similarly you could change it according to your need like in spite of printing lines which I did after checking $? status, where $? will check the exit status for last command.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 10-20-2015
Quote:
Originally Posted by RavinderSingh13
Hello erin00,

Could you please try following and let me know if this helps.
Code:
cat script.ksh
echo $1 | awk -F ':' '{match($0,/[0-2][0-4]:[0-5][0-9]:[0-5][0-9]/);A=substr($0,RSTART,RLENGTH);if(A && $1 !~ /24/){ print "good";exit 0}  else {print "bad";exit 1;}}'
if [[ $? == 0 ]]
then
     echo "we passed GOOD time test"
else
     exit;
fi

When I run the script with different arguments to check it's functionality it give following output then.
Code:
./script.ksh 24:17:12
bad
./script.ksh 23:17:12
good
we passed GOOD time test

Similarly you could change it according to your need like in spite of printing lines which I did after checking $? status, where $? will check the exit status for last command.

Thanks,
R. Singh
Well, this is great, but it does'nt allow times like 19:00:00 (because of
Code:
[0-2][0-4]

.

Last edited by erin00; 10-20-2015 at 09:18 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Valid separator in time and date format

Hello. I can use any particular (stupid or not) format when using bash date command. Example : ~> date --date "now" '+%Y-%m-%d %H!%M!%S' 2019-06-03 12!55!33or ~> date --date "now" '+%Y£%m£%d %H¤%M¤%S' 2019£06£03 12¤57¤36 or ~> date --date "now" '+%Y-%m-%d %H-%M-%S' 2019-06-03 12-58-51 ... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

Bash script accepting variables in valid number format

Hi Experts I would like to ask if there is a way to validate if the variable passed is in this kind of sample format "06-10" or "10-01". It was really a challenge to me on how to start and echnically the "6-10" stands for "June 10" and "10-01" stands as "October 1", overall it needs to have ... (3 Replies)
Discussion started by: ersan-poguita
3 Replies

3. Shell Programming and Scripting

Check for valid hostnames

Hello, I am trying to develop a script to check for valid hostnames. Below are the prerequisites for a valid hostname which I got from wiki : Hostnames are composed of series of labels concatenated with dots, as are all domain names. For example, "en.wikipedia.org" is a hostname. Each label... (8 Replies)
Discussion started by: rahul2662
8 Replies

4. UNIX for Dummies Questions & Answers

How to check if file contains valid strings?

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (5 Replies)
Discussion started by: rtagarra
5 Replies

5. Shell Programming and Scripting

how to check for valid password

I need to check if an account has a valid password. Would something like this work? read ACCNAME if grep -q "^$ACCNAME:\$6:" /etc/shadow; thenI noticed every entry in my shadow file that has a password starts with $6 ... it works for my current setup, but would it always work? I can't test... (4 Replies)
Discussion started by: ADay2Long
4 Replies

6. Homework & Coursework Questions

Bash shell - Check if value is valid directory.

1. The problem statement, all variables and given/known data: The script usage will be as follows: library.third source_directory - Your script will display an appropriate error message and exit with status 3 if no parameters are given - Your script will display an appropriate error... (2 Replies)
Discussion started by: netmaster
2 Replies

7. Shell Programming and Scripting

How create function valid birthday format dd-mm-yyyy

I write a small shell script create Payroll System my trouble is valid format birthday dd-mm-yyyy when you enter birthday if your value enter not match with format dd-mm-yyyy system will display "Fail Format Please re-enter Birthday with format dd-mm-yyyy" but i don't know how create function... (3 Replies)
Discussion started by: kency
3 Replies

8. Shell Programming and Scripting

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

9. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

10. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies
Login or Register to Ask a Question