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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Suraj
  • 75
  • 2
  • 13
  • 1
    Possible duplicate of http://stackoverflow.com/questions/18731346/validate-date-format-in-a-shell-script or http://stackoverflow.com/questions/21221562/bash-validate-date – codeforester Mar 03 '17 at 16:53
  • BTW, there are quoting issues here that http://shellcheck.net/ will catch. – Charles Duffy Mar 03 '17 at 16:59
  • i have tried this one also but not working – Suraj Mar 03 '17 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. – shellter Mar 03 '17 at 17:11

1 Answers1

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
ewcz
  • 12,819
  • 1
  • 25
  • 47
  • tnx lot its working fine – Suraj Mar 05 '17 at 13:37
  • Sir i want to add month also, then how i can do this ? – Suraj Mar 06 '17 at 04:34
  • @jhon and what is your input format? – ewcz Mar 06 '17 at 08:47
  • 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 – Suraj Mar 06 '17 at 10:43
  • @jhon I have included additional comments - I hope this explains the script better... – ewcz Mar 06 '17 at 19:29
  • Very very tnx sir – Suraj Mar 07 '17 at 05:53
  • Sir i have accepted the date time format in this way sir ./program.sh 2016-08-01 00:00:00 from the user . Now in script i want to divide this value such as date in another variable and time in another variable – Suraj Mar 08 '17 at 11:06
  • @jhon then it's even simpler because this is a format `date` understands. You can just use `s="${dtime}"` and then `x=$(date +'%Y-%m-%d %H:%M:%S' --date="$s" 2> /dev/null)`. The rest would stay the same. – ewcz Mar 08 '17 at 11:15
  • no not like that, i have acceppted date and time in this format ./program.sh 2016-08-01_00:00:00 and now i want to separte the date and time value in script. – Suraj Mar 08 '17 at 11:24
  • @jhon ok, in that case `s=${dtime/_/ }` and `x=$(date +'%Y-%m-%d_%H:%M:%S' --date="$s" 2> /dev/null)` – ewcz Mar 08 '17 at 11:26
  • or if you want to use the date/time later, you can do `datePart=$(date +'%Y-%m-%d' --date="$s")`. This will save the date into a variable `datePart`. One can proceed similarly for the time... – ewcz Mar 08 '17 at 11:27