Haris
Haris

Reputation: 14043

Shell Script Error

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 

Upvotes: 2

Views: 144

Answers (4)

aymericbeaumet
aymericbeaumet

Reputation: 7332

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 ]

Upvotes: 1

georgealton
georgealton

Reputation: 1039

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

Upvotes: 1

Davide
Davide

Reputation: 508

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

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

Should become

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

Upvotes: 2

Xaltar
Xaltar

Reputation: 1716

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 !

Upvotes: 1

Related Questions