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

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

我不知道这是不是坏事,也不知道这意味着什么。我的脚本似乎仍然运行良好,但我应该修复它吗?

#!/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]="[email protected]"

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

答案1

messages.sh:第 29 行:[:缺少‘]’

您正在使用以下内容:

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

但是上面的命令少了一个space]应该是:

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

基本规则

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

  1. 始终在括号和实际检查/比较之间保留空格。以下操作无效:

    if [$foo -ge 3]; then

    Bash 将会抱怨“缺少‘]’”。

来源Bash 脚本中的条件(if 语句)

答案2

您缺少一个空格。

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

另一个例子:

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

$ if [ "a" == "a" ]; then echo "yes"; else echo "no"; fi
yes

答案3

缺少前面的空格] 另外一种格式选项是:

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

相关内容