【问题标题】:Syntax error near附近有语法错误
【发布时间】:2013-06-22 23:42:46
【问题描述】:

很长一段时间以来,我一直在为这个小代码段而苦苦挣扎。

#!/bin/bash

testFunction() { 

        if [ -d "/home/$USER/.skandPATH/" ] ; then 
                #Exists. Do nothing. 
        else 
                mkdir /home/$USER/.skandPATH/   #Does not exist. Creates directory 
                addedPathDir=1 
        fi
} 

testFunction 

每次运行时都会收到以下消息:

./functionInScript.sh: line 7: syntax error near unexpected token `else'
./functionInScript.sh: line 7: `else '

我已经搜索了几十个“意外令牌 bash 附近的语法错误”并尝试了几十个建议。

我已经在文件上运行了 dos2unix(即使我使用的是 Mint)。 我已经逃脱了“.skandPATH”中的点,但这似乎没有什么区别。 我尝试删除每个空格字符和制表符。 我尝试注释掉行并执行脚本以尝试查明问题,但无济于事。 我正在竭尽全力试图找出这里出了什么问题!

如果你能帮助我...我将不胜感激。

【问题讨论】:

  • @Ignacio Vazquez-Abrams 它奏效了。我不知道在一个块中必须至少有一个命令。我很感激。谢谢!
  • 将来,您可能会发现shellcheck.net 有助于找出这些奇怪的 bash 错误。在这种情况下,它说“不能有空的 then 子句(使用 'true' 作为无操作)。”
  • @thatotherguy 感谢您的参考。看起来真的很有帮助。
  • @thatotherguy 谢谢。我从来没想到会有这样的网站存在。

标签: bash function if-statement token


【解决方案1】:

必须在一个块中至少有一个命令;试试:

【讨论】:

  • @thatotherguy 哇!我永远不会猜到这存在。谢谢!
【解决方案2】:

为什么不:

#!/bin/bash

testFunction() { 
    if ! [ -d "/home/$USER/.skandPATH/" ] ; then 
            mkdir /home/$USER/.skandPATH/
            addedPathDir=1
    fi
} 

testFunction 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多