messages.sh:第 29 行:[:缺少`]'

Jac*_*ob_ 6 bash bash-scripting

我不知道这是一件坏事,还是意味着什么。我的脚本似乎仍然可以正常工作,但我应该修复它吗?

#!/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
Run Code Online (Sandbox Code Playgroud)

Dav*_*ill 11

messages.sh:第 29 行:[:缺少 ']'

您正在使用以下内容:

if [ "alerttype" == "notification"]; then`
Run Code Online (Sandbox Code Playgroud)

但是,上面的命令缺少一个spacebefore ],应该是:

if [ "alerttype" == "notification" ]; then
                                  ^
Run Code Online (Sandbox Code Playgroud)

基本条件规则

当您开始编写和使用自己的条件时,您应该了解一些规则,以防止出现难以追踪的错误。以下是三个重要的:

  1. 始终在括号和实际检查/比较之间保留空格。以下将不起作用:

    if [$foo -ge 3]; then

    Bash 会抱怨“缺少']'”。

bash 脚本中的条件(if 语句)


Ste*_*ven 6

你少了一个空格。

#BEFORE
if [ "alerttype" == "notification"]; then
#AFTER
if [ "alerttype" == "notification" ]; then
#                                 ^
Run Code Online (Sandbox Code Playgroud)

另一个例子:

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

$ if [ "a" == "a" ]; then echo "yes"; else echo "no"; fi
yes
Run Code Online (Sandbox Code Playgroud)