6

I don't know if this is a bad thing, or what it means. My script still seems to work fine, but should I fix it?

#!/bin/sh
#This script will send text and maybe images to other computers via ssh and scp.
#Configuration files in same folder

source /Users/jacobgarby/Desktop/messaging/messages.cfg
TIME=$(date +"%H:%M:%S")
CONNECTED[0]="mainmini@192.168.1.65"

if [ -d messages.log ]; then
    :
else
    touch messages.log
fi

read MSG

if [ "$MSG" == "!help" ]; then
    echo ; echo "!clear   Clear's your personal chat log."
    echo "!ban [usrname]    Prevents a user from entering this chat IN DEV."
else
    echo "$TIME | $USER | $MSG" >> messages.log; echo   >> messages.log; echo   >> messages.log
    tail messages.log
fi

for CONNECTION in CONNECTED; do
    echo "It works"
done

if [ "alerttype" == "notification"]; then
    osascript -e 'display notification "You have recieved a message!" with title "Message"'
else
    osascript -e 'display dialog "You have recieved a message!" with title "Message"'
fi
10
  • Belongs on Linux/Unix site Jun 12 '15 at 18:43
  • The string alerttype will never be the same as the string notification. Perhaps you mean [ "$alerttype" = "notification" ]? Jun 12 '15 at 20:29
  • (and yes, I mean =, not == -- see the POSIX specification at pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html, and you'll see that the supported string comparison syntax is s1 = s2; == is a bash extension, and not all operating systems and shells will support it; if you want to use an explicitly bash-extended comparison operator rather than the POSIX-standard one, that's [[ ]], not [ ]). Jun 12 '15 at 20:31
  • Yeah, since I posted this I realized that
    – Jacob_
    Jun 12 '15 at 20:33
  • 1
    These sort of errors can be easily found with: shellcheck.net
    – SnakeDoc
    Jun 13 '15 at 1:54
11

messaging.sh: line 29: [: missing ']'

You are using the following:

if [ "alerttype" == "notification"]; then`

However, the above command is missing a space before ], it should be:

if [ "alerttype" == "notification" ]; then
                                  ^

The basic rules of conditions

When you start writing and using your own conditions, there are some rules you should know to prevent getting errors that are hard to trace. Here follow three important ones:

  1. Always keep spaces between the brackets and the actual check/comparison. The following won’t work:

    if [$foo -ge 3]; then

    Bash will complain about a "missing ']'".

Source Conditions in bash scripting (if statements)

5
  • I think Steven just said this, but you're right
    – Jacob_
    Jun 12 '15 at 15:48
  • Yeah, we answered around the same time ... I took longer because I wanted to look up and quote a reference for the correct syntax :/
    – DavidPostill
    Jun 12 '15 at 15:50
  • I know, I would upvote both but I don't have enough reputation
    – Jacob_
    Jun 12 '15 at 15:51
  • You seem to be half way to the "comment" privilege ;)
    – DavidPostill
    Jun 12 '15 at 15:56
  • It's more wrong than that. Even as [ "alerttype" == "notification" ], it's comparing two constants, meaning it will never be true. Perhaps one end or the other side should be a variable reference? Also, == isn't in the POSIX spec for test; to be portable, it needs to be =. Jun 12 '15 at 20:28
6

You're missing a single space.

#BEFORE
if [ "alerttype" == "notification"]; then
#AFTER
if [ "alerttype" == "notification" ]; then
#                                 ^

Another example:

$ if [ "a" == "a"]; then echo "yes"; else echo "no"; fi
-bash: [: missing `]'
no

$ if [ "a" == "a" ]; then echo "yes"; else echo "no"; fi
yes
1
  • 1
    It's more wrong than that. Even as [ "alerttype" == "notification" ], it's comparing two constants, meaning it will never be true. Also, == isn't in the POSIX spec for test; to be portable, it needs to be =. Jun 12 '15 at 20:28
2

Missing the space before the ] Also another format option is:

$ [ "a" == "a" ] && echo "yes" || echo "no"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.