简体   繁体   English

具有多个条件的 Bash IF 条件没有给出正确的结果

[英]Bash IF condition with multiple conditions not giving proper result

Trying to fetch the status of the URL for sonarqube quality gate and check if the status is "OK" the condition should pass or if the status is "ERROR" then it should fail.尝试获取 sonarqube 质量门的 URL 状态并检查状态是否为“OK”,条件是否应该通过,或者如果状态为“ERROR”,则它应该失败。

quality_gatesstatus=$(curl -u $SONAR_TOKEN:https://$SONAR_SERVER/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY\&pullRequest=$SONAR_PR_KEY | grep -Po '"status": *\K"[^"]*"')

echo $SONAR_PR_KEY

echo "Checking the Sonar Quality gate status"
if ("$quality_gatesstatus" != "OK") && ("$quality_gatesstatus" != "NONE")
then
echo "check sonar server and fix the issues: $quality_gatesstatus"
exit 1  
else
echo "Quality gate succeeded"
fi

But its not working as per the IF statement, its going always to the else condition但它不能按照 IF 语句工作,它总是进入 else 条件

The line:该行:

if ("$quality_gatesstatus" != "OK") && ("$quality_gatesstatus" != "NONE")

is evaluated as follows (not precisely, this is a heuristic):评估如下(不准确,这是一种启发式):

  1. the variable $quality_gatestatus is expanded to some string, say S变量$quality_gatestatus被扩展为一些字符串,比如S
  2. S is executed as a command, with the arguments != and OK S作为命令执行,带有参数!=OK
  3. If that command succeeds, then it is executed again with the arguments != and NONE .如果该命令成功,则使用参数!=NONE再次执行它。 If that command succeeds then the first block of commands is executed.如果该命令成功,则执行第一个命令块。 Otherwise, the commands in the else block are executed.否则,执行else块中的命令。

The error you are seeing is because the string S is not an executable command.您看到的错误是因为字符串S不是可执行命令。 Almost certainly what you actually want is:几乎可以肯定你真正想要的是:

if [ "$quality_gatesstatus" != OK ] && [ "$quality_gatesstatus" != NONE ]; then ...

but more likely you want a case statement:但更有可能你想要一个case陈述:

case "$quality_gatesstatus" in
OK) ... ;;
NONE) ... ;;
*) ... ;;
esac

The syntax of the shell is a bit counter-intuitive. shell 的语法有点违反直觉。 It is not if condition; then commands; fi不是if condition; then commands; fi if condition; then commands; fi if condition; then commands; fi . if condition; then commands; fi It is if commands; then commands; fi它是if commands; then commands; fi if commands; then commands; fi if commands; then commands; fi . if commands; then commands; fi In other words, when you write if [ 5 = 5 ] , the [ and ] are not part of the shell syntax.换句话说,当您编写if [ 5 = 5 ]时, []不是shell 语法的一部分。 Instead, the command [ is executed with arguments 5 , = , and ] .相反,命令[使用参数5=]执行。 Although [ is probably the most common command executed in an if block, it can be any set of commands, and it is common to see constructs like if grep ... or if shh ... or if curl ... .尽管[可能是在if块中执行的最常见的命令,但它可以是任何命令集,并且常见的结构是if grep ...if shh ...if curl ... It is slightly less common to see if cmd1; cmd2; cmd3; then ...查看if cmd1; cmd2; cmd3; then ...稍微不常见。 if cmd1; cmd2; cmd3; then ... if cmd1; cmd2; cmd3; then ... , but you will see it now and again. if cmd1; cmd2; cmd3; then ... ,但你会时不时地看到它。

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

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