Shell脚本的算术语法错误 [英] Arithmetic syntax error with shell script

查看:88
本文介绍了Shell脚本的算术语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果计算结果为TRUE,我将尝试调用函数"warn".我对语法仍然不太满意,想要一些有关如何修复最后一行的提示.

I am trying to call the function "warn" if the calculation is TRUE. I am still not quite comfortable with the syntax, would like some tips on how to fix the last line.

    if [ "$noproc" -gt 0 ]; then
    echo "WARNING: NoProc at $noproc for $process processes." >> $log

        elif [ "$max" -ge 11 ]; then 
        [ $(($max - $total)) -lt 6 && [ $idle -le $(($max \* 0.25 | bc -l)) ] ] | warn $total $process $max $idle

我得到的错误:第97行:[:缺少`]'

The error I get: line 97: [: missing ` ] '

推荐答案

如果您对此问题的标记正确,并且您真正使用的是bash(也就是说,脚本以#!/bin/开头bash ,或者如果不是通过shebang启动,则使用 bash yourscript 而不是 sh yourscript ),您也可以利用它.

If your tagging for this question is correct and you're genuinely using bash (which is to say that your script starts with #!/bin/bash, or if not started via a shebang you use bash yourscript rather than sh yourscript), you might as well take advantage of it.

# extended bash math syntax
if (( (max - total) < 6 )) && (( idle <= (max / 4) )); then
  warn "$total" "$process" "$max" "$idle"
fi

如果出于某种原因,您不想使用(()),则仍然可以使用 [[]] ,它为您提供了测试环境具有自己的扩展语法:

If, for whatever reason, you don't want to use (( )), you can still use [[ ]], which gives you a test context with its own extended syntax:

# extended bash test syntax
if [[ $((max - total)) -lt 6 && $idle -le $(bc -l <<<"$max*0.25") ]]; then
  warn "$total" "$process" "$max" "$idle"
fi

...如果您想与POSIX sh兼容,则需要结束测试,然后才能放入Shell级的逻辑AND运算符.

...whereas if you want to be compatible with POSIX sh, you need to end the test before you can put in a shell-level logical-AND operator.

# corrected POSIX-compliant test syntax
if [ "$((max - total))" -lt 6 ] && [ "$idle" -le "$(bc -l <<<"$max*0.25")" ]; then
  warn "$total" "$process" "$max" "$idle"
fi


要了解原因,让我们看看如果将(完全不正确的) | 符号改为&&,则原始命令将如何解析:


To understand why, let's look at how your original command would parse, if you changed the (utterly incorrect) | symbol to && instead:

# Equivalent (longer form) of the original code, with pipe corrected to logical-AND
if [ $(($max - $total)) -lt 6; then
  if [ $idle -le $(($max \* 0.25 | bc -l)) ] ]; then
    warn $total $process $max $idle
  fi
fi

请注意,它正在运行,作为单个命令 [$((($ max-$ total))-lt 6 .

Note that this is running, as a single command, [ $(($max - $total)) -lt 6.

[不是特殊的Shell语法-只是命令.在较旧的Shell中,它实际上是/usr/bin/[;今天,还内置了 [,但是除了执行速度更快之外,它的行为方式与执行旧的外部命令的方式完全相同.

[ is not special shell syntax -- it's just a command. In older shells it was actually /usr/bin/[; today, there's also a [ builtin as well, but other than being faster to execute, it behaves exactly the same way as it would have were it executing the old, external command.

[命令希望传递一个] 作为其最后一个参数,因为在 -lt 6之后没有] ,您会收到语法错误,然后退出.

That [ command expects to be passed a ] as its last argument, since there's no ] after the -lt 6, you get a syntax error and it exits.

类似地,您的代码将(如果第一个命令成功执行)运行 [$ idle -le $((($ max \ * 0.25 | bc -l))]] .在这里,您有一个 [命令最后传递了两个 ] ;它根本不知道该如何处理第二个.

Similarly, your code would then (if the first command succeeded) run [ $idle -le $(($max \* 0.25 | bc -l)) ] ]. Here, you have a [ command passed two ]s on the end; it simply doesn't know what to do with the second one.

这篇关于Shell脚本的算术语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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