为什么我会得到一个错误“预期一元运算符”? [英] Why am I getting a 'unary operator expected' error?

查看:98
本文介绍了为什么我会得到一个错误“预期一元运算符”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个shell脚本,以简化我的开发流程。

这需要一个参数去哪家主题文件夹,我要在工作和启动咕噜观看在该目录中。

如果我调用脚本没有我目前正在打印该主题需要指定作为命令行参数的警告,必要的参数。

我想打印的可用选项的列表,例如主题目录

这是我迄今为止...

 主题= $ 1如果[$ THEME =='']
然后
    回声需要指定主题
其他
    CD工作空间/ AWS /鬼/'$主题'/'
    咕噜手表
科幻

在理想情况下我会代替输出的回声与主题父目录的 LS像这样一行

 主题= $ 1如果[$ THEME =='']
然后
    回声'需要从以下指定主题为
    LS工作区/ AWS /鬼
其他
    CD工作空间/ AWS /鬼/'$主题'/'
    咕噜手表
科幻

然而,这给了我下面的错误

  ./ ghost_dev.sh:3号线:[:==:一元运算符预期


解决方案

您需要大约 $主题这里报价:

 如果[$ THEME =='']

否则,当你不指定一个主题, $主题扩展到什么,外壳看到这个语法错误:

 如果[=='']

通过补充说,像这样的报价:

 如果[$主题为=='']

一个空 $主题的扩张得到有效的这种比较,而不是:

 如果[=='']

本容量运行时的语法错误,可奇怪那些其背景是在更传统的编程语言,但命令shell(至少那些在伯恩传统)解析code有些不同。在很多情况下,外壳参数更像宏不是变量;这种行为提供了灵活性,但也创造了陷阱粗心。

既然你标记这个问题的庆典,值得注意的是有关于在bash提供的新测试语法内部参数扩展的结果,执行无字分裂(和ksh / zsh中) ,即 [ ... ]] 。所以,你也可以这样做:

 如果[[$ THEME =='']

您可以逃脱的地方不加引号上市 rel=\"nofollow\">。但它是一个很好的习惯总是报价参数扩展反正除非要明确字分裂(即使如此,看看,如果阵列将解决你的问题,而不是)。

这将是更地道,而不是使用与空字符串平等 -z 测试操作:

 如果[-z$主题为]

此外,既不 [ ... ]] 还是 == 运营商是POSIX标准的一部分,所以如果你关心与其他POSIX炮弹严格的兼容性,坚持引用的解决方案并使用 -z 或一个 =

I'm writing a shell script to streamline my development workflow.

It takes an argument as to which theme folder I'm going to be working in and starts grunt watch on that directory.

If I call the script without the necessary argument I'm currently printing a warning that a theme needs to be specified as a command line argument.

I'd like to print a list of the available options, e.g. theme directories

This is what I have so far...

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme'
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

Ideally I'd replace the output of the echo line with an ls of the themes parent directory like so

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme from the following'
    ls workspace/aws/ghost
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

However this gives me the following error

./ghost_dev.sh: line 3: [: ==: unary operator expected

解决方案

You need quotes around $THEME here:

if [ $THEME == '' ]

Otherwise, when you don't specify a theme, $THEME expands to nothing, and the shell sees this syntax error:

if [ == '' ]

With quotes added, like so:

if [ "$THEME" == '' ]

the expansion of an empty $THEMEyields this valid comparison instead:

if [ "" == '' ]

This capacity for runtime syntax errors can be surprising to those whose background is in more traditional programming languages, but command shells (at least those in the Bourne tradition) parse code somewhat differently. In many contexts, shell parameters behave more like macros than variables; this behavior provides flexibility, but also creates traps for the unwary.

Since you tagged this question bash, it's worth noting that there is no word-splitting performed on the result of parameter expansion inside the "new" test syntax available in bash (and ksh/zsh), namely [[...]]. So you can also do this:

if [[ $THEME == '' ]]

The places you can get away without quotes are listed here. But it's a fine habit to always quote parameter expansions anyway except when you explicitly want word-splitting (and even then, look to see if arrays will solve your problem instead).

It would be more idiomatic to use the -z test operator instead of equality with the empty string:

if [ -z "$THEME" ]

Also, neither [[...]] nor the == operator are part of the POSIX standard, so if you care about strict compatibility with other POSIX shells, stick to the quoting solution and use either -z or a single =.

这篇关于为什么我会得到一个错误“预期一元运算符”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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