Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash比较_Bash_Comparison_Floating Point - Fatal编程技术网

Bash比较

Bash比较,bash,comparison,floating-point,Bash,Comparison,Floating Point,我有一个存储命令输出的变量。我如何将其与浮动进行比较 更具体地说,我正在做 x=$(tail -n 1 foo| cut -d ' ' -f2) if (($x < 0)); then ... 我需要比较的值是-0.08,但错误标记不同 对于这种比较,我应该怎么做?bash不支持浮点算术。 但是,您可以使用bc,这是一个执行算术运算的外部程序 if (( $(bc <<< "$x < 0") )); then printf &quo

我有一个存储命令输出的变量。我如何将其与浮动进行比较

更具体地说,我正在做

x=$(tail -n 1 foo| cut -d ' ' -f2)

if (($x < 0)); then ...
我需要比较的值是
-0.08
,但错误标记不同


对于这种比较,我应该怎么做?

bash不支持浮点算术。
但是,您可以使用
bc
,这是一个执行算术运算的外部程序

if (( $(bc <<< "$x < 0") )); then 
    printf "%f is less than 0\n" "$x"; 
fi

if($(bcbash不支持浮点运算。
但是,您可以使用
bc
,这是一个执行算术运算的外部程序

if (( $(bc <<< "$x < 0") )); then 
    printf "%f is less than 0\n" "$x"; 
fi

if($(bc如果您可以使用ksh,您可以使用它来编写脚本,而不是Bash,因为它支持浮动。Zsh也支持浮动

#!/usr/bin/ksh
x=$(tail -n 1 foo| cut -d ' ' -f2)

if ((x < 0))
then
    echo "less than"
fi
!/usr/bin/ksh
x=$(尾部-n 1 foo |切割-d'-f2)
如果((x<0))
然后
回声“小于”
fi

如果您可以使用ksh,您可以使用它来编写脚本,而不是使用Bash,因为它支持浮动。Zsh也支持浮动

#!/usr/bin/ksh
x=$(tail -n 1 foo| cut -d ' ' -f2)

if ((x < 0))
then
    echo "less than"
fi
!/usr/bin/ksh
x=$(尾部-n 1 foo |切割-d'-f2)
如果((x<0))
然后
回声“小于”
fi