简体   繁体   English

错误 [: ==: shell 脚本中需要一元运算符

[英]Error [: ==: unary operator expected in shell script

I am trying to run shell script in which I am matching two string values but getting above error.我正在尝试运行 shell 脚本,其中我匹配两个字符串值但出现错误。 Can you please help me how to resolve this?你能帮我解决这个问题吗?

Example:例子:

run command:运行命令:

sh script.sh AMPIL Group_1

So here Group_1 is a Execution_order所以这里Group_1是一个Execution_order

I am using below shell script:我在下面使用 shell 脚本:

    if [ $Execution_Order == "Group_1" ]
    then
        rm sqoop_queries_meta_data_Group_1.txt
        echo $sqoop_queries >> sqoop_queries_meta_data_Group_1.txt
    elif [ $Execution_Order == "Group_2" ]
    then
        rm sqoop_queries_meta_data_Group_2.txt
        echo $sqoop_queries >> sqoop_queries_meta_data_Group_2.txt
    else 
        rm sqoop_queries_meta_data.txt
        echo $sqoop_queries >> sqoop_queries_meta_data.txt
    fi

If else part is not working and giving the above error.如果其他部分不起作用并给出上述错误。 Can you please correct me?你能纠正我吗?

The string comparison operator of the test command, (ie the [... ] syntax is defined as a single equals sign. Some shells (including bash ) also accept the double-equals but it's not part of the original POSIX spec. test命令的字符串比较运算符(即[... ]语法被定义为单个等号。一些 shell(包括bash )也接受双等号,但它不是原始 POSIX 规范的一部分。

Thus, you might want to change your queries to:因此,您可能希望将查询更改为:

if [ "$Execution_Order" = "Group_1" ]
then
    rm sqoop_queries_meta_data_Group_1.txt
    echo "$sqoop_queries" >> sqoop_queries_meta_data_Group_1.txt
elif [ "$Execution_Order" = "Group_2" ]
then
    rm sqoop_queries_meta_data_Group_2.txt
    echo "$sqoop_queries" >> sqoop_queries_meta_data_Group_2.txt
else 
    rm sqoop_queries_meta_data.txt
    echo "$sqoop_queries" >> sqoop_queries_meta_data.txt
fi

I don't see why you need an elif in this special example.在这个特殊的例子中,我不明白为什么你需要一个elif Also, I don't understand why you first remove a file and then append to that very file.另外,我不明白你为什么先删除一个文件,然后再删除 append 到那个文件。 How about怎么样

eo="${Execution_Order:+_$Execution_Order}"
echo "$sqoop_queries" > "sqoop_queries_meta_data$eo.txt"

? ? The main difference is that if Execution_Order would be, say, Group_3, it would create a file sqoop_queries_meta_data_Group3 in my solution, while it would write to sqoop_queries_meta_data.txt in your attempt, but if this is a problem in your context, this can easily be fixed.主要区别在于,如果Execution_Order是 Group_3,它将在我的解决方案中创建一个文件 sqoop_queries_meta_data_Group3,而它会在您的尝试中写入 sqoop_queries_meta_data.txt,但如果这是您的上下文中的问题,这很容易固定的。

Explanation: The :+ sets eo to empty, if Execution_Order is empty, and to _$Execution_Order otherwise.说明: :+eo设置为空,如果Execution_Order为空,否则设置为_$Execution_Order

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM