Bash 中预期的一元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22798333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:05:03  来源:igfitidea点击:

Unary operator expected in Bash

bashintegeroperator-keyword

提问by Paolo

I've seen questions regarding the same issue, but all of them are about strings. How about integers, why am I getting the "unary operator expected" error?

我看过有关同一问题的问题,但所有问题都与字符串有关。整数怎么样,为什么我会收到“预期的一元运算符”错误?

 if [ $(date +%k%M) -ge ${!BLOCK1FRAN} ] ; then whatever ; fi

回答by alvits

You are using indirection. If the variable ${BLOCK1FRAN}points to an empty variable, you'll get the error message. Make sure that the variable pointed by ${BLOCK1FRAN}contains valid numeric value.

您正在使用间接。如果变量${BLOCK1FRAN}指向一个空变量,您将收到错误消息。确保指向的变量${BLOCK1FRAN}包含有效的数值。

If you want empty string and non numeric values to be evaluated as zero 0use the following syntax.

如果您希望将空字符串和非数字值计算为零,请0使用以下语法。

if [[ $(date +%k%M) -ge ${!BLOCK1FRAN} ]]; then whatever ; fi

回答by that other guy

Looks good to me. Are you sure you've set BLOCK1FRAN correctly?

在我看来很好。你确定你已经正确设置了 BLOCK1FRAN 吗?

$ whatever() { echo "it works"; }
$ foo=42
$ BLOCK1FRAN=foo
$ if [ $(date +%k%M) -ge ${!BLOCK1FRAN} ] ; then whatever ; fi
it works