bash脚本中的[:缺少`]' [英] [: missing `]' in bash script

查看:2210
本文介绍了bash脚本中的[:缺少`]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在编写一个bash shell脚本,我的前几行看起来像这样:

So I'm writing a bash shell script, and my first few lines looks like this:

if ! [ $# -eq 0 || $# -eq 1 ]; then
    echo -e "Usage: myScriptName [\e[3mdir\e[0m] [\e[3m-f file\e[0m]"
    exit 1
fi

但是当我运行它时,它说"[:缺少`]'".我没有看到[],除了;之外,什么也没有;正在触摸],那么我想念什么?

But when I run it, it says "[: missing `]'". I don't see a missing ], and nothing except the ; is touching the ], so what am I missing?

推荐答案

您不能在单括号测试表达式中使用像||这样的运算符.您必须要么

You cannot use operators like || within single-brace test expressions. You must either do

! [[ $# -eq 0 || $# -eq 1 ]]

! { [ $# -eq 0 ] || [ $# -eq 1 ]; }

! [ $# -eq 0 -o $# -eq 1 ]

double-brace关键字是bash表达式,不能与其他POSIX shell一起使用,但是它也有一些好处,例如能够更可读地执行这些操作.

The double-brace keyword is a bash expression, and will not work with other POSIX shells, but it has some benefits, as well, such as being able to do these kinds of operations more readably.

当然,有很多方法可以测试传递的参数数量. $2的存在也将回答您的问题.

Of course, there are a lot of ways to test the number of arguments passed. The mere existence of $2 will answer your question, as well.

这篇关于bash脚本中的[:缺少`]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆