bash shell 脚本语法错误

标签 bash scripting syntax-error

这个问题在这里已经有了答案:





Why should there be spaces around '[' and ']' in Bash?

(5 个回答)


6年前关闭。




我在 bash 脚本中编写了一个函数。但是,它提示语法。我真的看不到它是什么.....错误消息是[:缺少`]'

addem() {
            if [ $# -eq 0] || [ $# -gt 2 ]
            then
                    echo -1
            elif [ $# -eq 1 ]
            then
                    echo $[ $1 + $1 ]
            else
                    echo $[ $1 + $2 ]
            fi
    }

最佳答案

在第一个 ] 之前需要一个空格.那是:
改变:if [ $# -eq 0] || [ $# -gt 2 ]到:if [ $# -eq 0 ] || [ $# -gt 2 ]

关于bash shell 脚本语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1035396/

相关文章:

Bash 脚本 : Send files to SFTP using Expect

Bash:循环遍历文件中的行并使用每行的索引 ('enumerate' 相当于 python 中的索引)

bash - grep 多个文件

c - Libev编译时报错

linux - bash 脚本中的错误替换错误

linux - 运行简单 shell 脚本时出错

用于 Redis 的脚本语言

ruby - Selenium::WebDriver::Error::UnknownError:未知错误:找不到 Chrome 二进制文件(驱动程序信息:chromedriver=2.31.488774)

python - 使用python向表中添加列的MySQL语法错误

linux - 使用 [] 的参数太多