2

Can any one tell what wrong with this script ?. Because I am getting the error like

./timer: line 9: [13: command not found
./timer: line 12: [13: command not found

My script look like

#!/bin/bash

   while :
     do
    HOUR=$(date +%H)
    MINUTE=$(date +%M)
    SECOND=$(date +%S)

        if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]
     then ./binary

    elif [$HOUR == 18] && [$MINUTE == 30] && [$SECOND == 1] 
     then ./binary
    fi

    done 

4 Answers 4

2

put a space between the [ ... ] Example:

if [$HOUR == 13] && [$MINUTE == 12] && [$SECOND == 1]

Should become

if [ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]
Sign up to request clarification or add additional context in comments.

1 Comment

== isn't POSIX, the correct operator to compare string is =. However, we don't wan't to compare strings but integers, so we shall use -eq instead. Take a look at my answer for more information.
1

I think you must use "${VARIABLE}" and respect spaces for square brackets

This would give :

if [ "${HOUR}" == 13 ] && [ "${HOUR}" == 12 ] && [ "${HOUR}" == 1 ]

Hope that helps !

Comments

1

the test operators in bash need to have a space by the opening and closing bracket, try

[ $HOUR == 13 ] && [ $MINUTE == 12 ] && [ $SECOND == 1 ]

and

[ $HOUR == 18 ] && [ $MINUTE == 30 ] && [ $SECOND == 1 ]

here is a link that you might find useful http://tldp.org/LDP/abs/html/testconstructs.html

Comments

1

Several things:

  • You have to put a space after [ and before ].
  • It's a good practice to protect your variables using double quotes.
  • You have to use the -eq operator to compare numeric values (see Bash conditional operators).

Like so:

if [ "$HOUR" -eq 13] && [ "$MINUTE" -eq 12 ] && [ "$SECOND" -eq 1 ]

elif [ "$HOUR" -eq 18 ] && [ "$MINUTE" -eq 30 ] && [ "$SECOND" -eq 1 ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.