-1

我正在尝试编写脚本。

这是我的代码的一部分

if [ #? -ne 0 ]
then
    echo "Args:"

    if [ -r $word.args ]
    then
        cat $word.args
    fi

    echo "Expected:"
    cat $word.out
    echo "Actual:"
    cat $tempfile
fi

当我运行一些测试时,它总是在这一行显示“[: missing `]'”

if [ #? -ne 0 ]

为什么?

4

1 回答 1

1

这是因为行中有语法错误:

if [ #? -ne 0 ]

要更正它,请将其更改为:

# If you are interested in getting number of arguments
if [ $# -ne 0 ]

或者

# If you are interested in getting exit status code of last command i.e.
# whether or not the last command executed successfully
if [ $? -ne 0 ]

注意#用于行注释。因此,它会忽略#直到该行末尾的所有文本。

您会发现此链接很有用

于 2019-09-27T18:58:15.297 回答