Messaging. Sh: Line 29: [: Missing `]'

Messaging. Sh: Line 29: [: Missing `]'

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

3 Answers

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

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

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

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.