0

I want to do something if the date is in between 08:03 an 14:03 in a script.

but when I try

dt=`date +%H:%M`


if [ $dt -eq 08:03 ]
then
echo $dt > msize.txt
#echo "iam im 1 if"
fi

it doesn't work. This file is run in crontab in every minute. Please suggest something

dragfyre
  • 452
  • 4
  • 11
Sunil Sahoo
  • 23
  • 1
  • 6

2 Answers2

3

As Rafe points out in the other answer, -eq is numeric, you need to use == to compare strings. But you said that you want to tell if the time is between two times, not exactly equal to one. In that case you should use the < operator:

if [ '08:03' \< "$dt" -a "$dt" \< '14:03']

Or more conveniently:

if [[ '08:03' < "$dt" && "$dt" < '14:03']]

Note that these operators are not specified in POSIX, but work on most shells (Bash, Korn Shell, zsh). Just beware of using them if you're using a minimal shell like Dash (which is what /bin/sh is on Ubuntu).

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
2

The -eq operator is for comparing integers, not strings. If you want to compare strings, you'll need to use ==. Try this:

if [ $dt == '08:03' ]
then
# Rest of script
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • @glenn right, this is assuming that he uses bash (which is what pretty much all Linux systems use, unless they use zsh, which is the same). This works on pretty much everything but ksh. – Rafe Kettler May 20 '11 at 18:05