Shell script for date manipulation
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 fil开发者_JS百科e is run in crontab in every minute. Please suggest something
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).
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
精彩评论