一元运算符预期 [英] unary operator expected

查看:129
本文介绍了一元运算符预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚什么是错与此,但就是无法弄清楚..

这是部分似乎得到一个错误。

 的elif [$操作=人];然后
    如果[$ AUG1 =添加];然后#< - 75号线
    回声手册页:加上
    回声
    回声语法:加[数字1] [2号]
    回声
    回声简介:
    回声添加两个不同的数字加在一起。
    回声
    回声信息:
    回声中新增V1.0
    回声
ELIF [-z $ AUG1]。然后
    回声请指定一个命令来阅读man page。
其他
    回声有该命令没有手册页。
科幻

我得到这个错误:

  calc_1.2:75行:[:=:一元运算符预期


解决方案

如果你知道你总是要使用bash,它更容易为始终使用双支架有条件的复合命令 [。 ..] ,而不是符合POSIX标准的单支架版本 [...] 。在 [...] 复合,字的拆分和路径扩展不会应用到的话,那么你可以依靠

 如果[[$ AUG1 ==和]];

$ AUG1 的值与字符串比较

如果您使用 [...] ,你总是需要记住翻一番这样的引用变量:

 如果[$ AUG1=和];

如果你不引用变量扩展和变量未定义或空的,它从犯罪现场消失了,只留下

 如果[=和];

这不是有效的语法。 (这也将失败,不同的​​错误消息,如果 $ AUG1 包含空格或shell转义字符。)

现代 [经营者有很多其他不错的功能,包括常规的前pression匹配。

I've been trying to figure out whats wrong with this but just can't figure it out..

This is the part seems to be getting an error..

elif [ $operation = "man" ]; then
    if [ $aug1 = "add" ]; then         # <- Line 75
    echo "Man Page for: add"
    echo ""
    echo "Syntax: add [number 1] [number 2]"
    echo ""
    echo "Description:"
    echo "Add two different numbers together."
    echo ""
    echo "Info:"
    echo "Added in v1.0"
    echo ""
elif [ -z $aug1 ]; then
    echo "Please specify a command to read the man page."
else
    echo "There is no manual page for that command."
fi

I get this error:

calc_1.2: line 75: [: =: unary operator expected

解决方案

If you know you're always going to use bash, it's much easier to always use the double bracket conditional compound command [[ ... ]], instead of the Posix-compatible single bracket version [ ... ]. Inside a [[ ... ]] compound, word-splitting and pathname expansion are not applied to words, so you can rely on

if [[ $aug1 == "and" ]];

to compare the value of $aug1 with the string and.

If you use [ ... ], you always need to remember to double quote variables like this:

if [ "$aug1" = "and" ];

If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only

if [ = "and" ]; 

which is not a valid syntax. (It would also fail with a different error message if $aug1 included white space or shell metacharacters.)

The modern [[ operator has lots of other nice features, including regular expression matching.

这篇关于一元运算符预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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