【问题标题】:compare variables irrespective of signage in bash比较变量而不考虑 bash 中的标志
【发布时间】:2021-02-10 03:59:58
【问题描述】:

我想比较bash中的两个变量,看看这个变量是否超过了cut_off值。

例如

截止值 = 500.00

cut_off=500.00

现在我想检查一个变量,无论其标志是否超过cut_off

source_1=-510.00
target_1=1620.00

source_2=490.00
target_2=-488.13

source_3=5490.00
target_3=-4488.13

现在,当要检查的变量大于 cut_off 然后 echo 说 cut_off 的 variable value is varying by differenec - 要检查的变量`

我已经完成了下面的操作,但它没有打印出正确的错误声明

cutoff=500.0

actual_diff=$(echo "${source}" - "${target}" | bc)

if (( $(echo "$actual_diff > $cutoff" |bc -l) ));
then
    echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
    exit 1
else
    echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi

【问题讨论】:

  • 什么是截止范围? 0 到截止?
  • @kalou.net 在-500.00 to 500.00之间
  • 所以,如果我理解正确,您想将 actual_diff 的“绝对值”与 cut_off 的“绝对值”进行比较 ..
  • @kalou.net 是的,这是正确的

标签: linux bash


【解决方案1】:

好的,这就是我的提议;

cutoff=500.0

actual_diff=$(echo "${source}" - "${target}" | bc)

if (( $(echo "${actual_diff#-} > ${cutoff#-}" |bc -l) ));

then
    echo -e "Difference between source and target is more than cut_off Actual difference is ${actual_diff}"
    exit 1
else
    echo -e "Difference between source and target is in cut_off range Actual difference is ${actual_diff}"
fi

(感谢下面的帖子Absolute value of a number

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    相关资源
    最近更新 更多