如何在linux shell脚本中将变量与变量减去常量进行比较?

时间:2021-02-11 23:53:21

I want to compare a variable with another variable minus a constant in a linux shell script.

我想比较一个变量与另一个变量减去linux shell脚本中的常量。

In cpp this would look like this:

在cpp中,这将是这样的:

int index = x;
int max_num = y;

if (index < max_num - 1) {
  // do whatever
} else {
  // do something else
}

In the shell i tried the following:

在shell中我尝试了以下内容:

  index=0
  max_num=2
  if [ $index -lt ($max_num - 1) ]; then
    sleep 20
  else 
    echo "NO SLEEP REQUIRED"
  fi

I also tried:

我也尝试过:

if [ $index -lt ($max_num-1) ]; then
...

if [ $index -lt $max_num - 1 ]; then
...

if [ $index -lt $max_num-1 ]; then
...

but non of these versions works. How do you write such a condition correctly?

但这些版本不起作用。你怎么正确地写这样的条件?

Regards

3 个解决方案

#1


7  

The various examples that you tried do not work because no arithmetic operation actually happens in any of the variants that you tried.

您尝试的各种示例都不起作用,因为在您尝试的任何变体中都没有实际发生算术运算。

You could say:

你可以说:

if [[ $index -lt $((max_num-1)) ]]; then
  echo yes
fi

$(( expression )) denotes Arithmetic Expression.

$((表达式))表示算术表达式。

[[ expression ]] is a Conditional Construct.

[[expression]]是一个条件构造。

#2


2  

In bash, a more readable arithmetic command is available:

在bash中,可以使用更具可读性的算术命令:

index=0
max_num=2
if (( index < max_num - 1 )); then
  sleep 20
else 
  echo "NO SLEEP REQUIRED"
fi

The strictly POSIX-compliant equivalent is

严格遵守POSIX的等价物

index=0
max_num=2
if [ "$index" -lt $((max_num - 1)) ]; then
  sleep 20
else 
  echo "NO SLEEP REQUIRED"
fi

#3


2  

Portably (plain sh), you could say

你可以说,可以轻松地(简单地说)

if [ "$index" -lt "$((max_num-1))" ]; then
    echo yes
fi

Short version

[ "$index" -lt "$((max_num-1))" ] && echo yes;

[ is the test program, but requires the closing ] when called as [. Note the required quoting around variables. The quoting is not needed when using the redundant and inconsistent bash extensions cruft ([[ ... ]]).

当被称为[时,[是测试程序,但需要关闭]。请注意变量所需的引用。使用冗余和不一致的bash扩展cruft([[...]])时不需要引用。

#1


7  

The various examples that you tried do not work because no arithmetic operation actually happens in any of the variants that you tried.

您尝试的各种示例都不起作用,因为在您尝试的任何变体中都没有实际发生算术运算。

You could say:

你可以说:

if [[ $index -lt $((max_num-1)) ]]; then
  echo yes
fi

$(( expression )) denotes Arithmetic Expression.

$((表达式))表示算术表达式。

[[ expression ]] is a Conditional Construct.

[[expression]]是一个条件构造。

#2


2  

In bash, a more readable arithmetic command is available:

在bash中,可以使用更具可读性的算术命令:

index=0
max_num=2
if (( index < max_num - 1 )); then
  sleep 20
else 
  echo "NO SLEEP REQUIRED"
fi

The strictly POSIX-compliant equivalent is

严格遵守POSIX的等价物

index=0
max_num=2
if [ "$index" -lt $((max_num - 1)) ]; then
  sleep 20
else 
  echo "NO SLEEP REQUIRED"
fi

#3


2  

Portably (plain sh), you could say

你可以说,可以轻松地(简单地说)

if [ "$index" -lt "$((max_num-1))" ]; then
    echo yes
fi

Short version

[ "$index" -lt "$((max_num-1))" ] && echo yes;

[ is the test program, but requires the closing ] when called as [. Note the required quoting around variables. The quoting is not needed when using the redundant and inconsistent bash extensions cruft ([[ ... ]]).

当被称为[时,[是测试程序,但需要关闭]。请注意变量所需的引用。使用冗余和不一致的bash扩展cruft([[...]])时不需要引用。