0

I want to accept date time value from passing parameters through user and wants to validate that in shell script. How I can do that, I have tried in following way but failed:

./program.sh 12-10:12:11 (In this 12 is date and other is time) 

I am passing this as parameter and accepting it in shell script

dtime=$1 if ! [ "`date '+%d-%H:%M:%S' -d $dtime 2>/dev/null`" = "$dtime" ] then echo $dtime is NOT a valid date format, use the d-H:M:S format exit 0 fi echo $dtime 

But it's showing date is not valid.

4
  • 1
    Possible duplicate of stackoverflow.com/questions/18731346/… or stackoverflow.com/questions/21221562/bash-validate-date Commented Mar 3, 2017 at 16:53
  • BTW, there are quoting issues here that shellcheck.net will catch. Commented Mar 3, 2017 at 16:59
  • i have tried this one also but not working Commented Mar 3, 2017 at 17:02
  • 1
    you're defeating your ability to debug by sending date output to 2>/dev/null. Try dtime=$1 ; date '+%d-%H:%M:%S' -d "$dtime"; echo status=$? and if you get the processing you expect. Good luck. Commented Mar 3, 2017 at 17:11

1 Answer 1

3

The thing is that the format supplied to date command controls only the output, not the input (passed with the --date option). One needs to supply some format which date will understand. For example, one could manually replace the dash in the input with a space, prepend the current year/month and use this modified string for testing:

#!/usr/bin/env bash #date/time string is passed as first argument dtime=$1 #replace first occurrence of - in $dtime with space #and prepend the current year/month/ #This will for example #transform the input "12-10:12:11" to "2017/03/12 10:12:11", i.e., #into a format which `date` understands. The reason for this is #to provide complete date specification, otherwise `date` #would complain that the date is invalid. s=$(date +'%Y/%m/')${dtime/-/ } #feed the transformed input obtained in previous step to the #`date` command and print the output in the required '%d-%H:%M:%S' format x=$(date +'%d-%H:%M:%S' --date="$s" 2> /dev/null) #finally, check if this formatted value equals the original input or not if [ "${x}" != "${dtime}" ] then echo "$dtime is NOT a valid date format, use the d-H:M:S format" exit 0 fi echo $dtime 
Sign up to request clarification or add additional context in comments.

11 Comments

tnx lot its working fine
Sir i want to add month also, then how i can do this ?
@jhon and what is your input format?
sir the above your code is working fine for me but i am not able to understand this code. Can you please give me the explaination of this code dtime=$1 s=$(date +'%Y/%m/')${dtime/-/ } x=$(date +'%d-%H:%M:%S' --date="$s" 2> /dev/null) if [ "${x}" != "${dtime}" ] then echo "$dtime is NOT a valid date format, use the d-H:M:S format" exit 0 fi echo $dtime
@jhon I have included additional comments - I hope this explains the script better...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.