在 bash shell 脚本中,如何将字符串转换为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1786888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:43:18  来源:igfitidea点击:

In bash shell script how do I convert a string to an number

bashshell

提问by PJT

Hey I would like to convert a string to a number

嘿,我想将字符串转换为数字

x="0.80"

#I would like to convert x to 0.80 to compare like such:
if[ $x -gt 0.70 ]; then

echo $x >> you_made_it.txt 

fi 

Right now I get the error integer expression expected because I am trying to compare a string.

现在我得到了预期的错误整数表达式,因为我正在尝试比较一个字符串。

thanks

谢谢

采纳答案by William Pursell

For some reason, this solution appeals to me:

出于某种原因,这个解决方案对我很有吸引力:

if ! echo "$x $y -p" | dc | grep > /dev/null ^-; then
  echo "$x > $y"
else
  echo "$x < $y"
fi

You'll need to be sure that $x and $y are valid (eg contain only numbers and zero or one '.') and, depending on how old your dc is, you may need to specify something like '10k' to get it to recognize non-integer values.

您需要确保 $x 和 $y 是有效的(例如只包含数字和零或一个“.”),并且,根据您的 dc 的年龄,您可能需要指定诸如“10k”之类的内容来获得它识别非整数值。

回答by ghostdog74

you can use bc

你可以使用 bc

$ echo "0.8 > 0.7" | bc
1
$ echo "0.8 < 0.7" | bc
0
$ echo ".08 > 0.7" | bc
0

therefore you can check for 0 or 1 in your script.

因此,您可以在脚本中检查 0 或 1。

回答by Warren Young

Bash doesn't understand floating-point numbers. It only understands integers.

Bash 不理解浮点数。它只理解整数。

You can either step up to a more powerful scripting language (Perl, Python, Ruby...) or do all the math through bcor similar.

您可以升级到更强大的脚本语言(Perl、Python、Ruby...),或者通过bc或类似的方式完成所有数学运算。

回答by Paused until further notice.

If your values are guaranteed to be in the same form and range, you can do string comparisons:

如果您的值保证在相同的形式和范围内,您可以进行字符串比较:

if [[ $x > 0.70 ]]
then
    echo "It's true"
fi

This will fail if xis ".8" (no leading zero), for example.

x例如,如果是“.8”(没有前导零),这将失败。

However, while Bash doesn't understand decimals, its builtin printfcan format them. So you could use that to normalize your values.

然而,虽然 Bash 不理解小数,但它的内置printf函数可以格式化它们。所以你可以用它来规范你的价值观。

$ x=.8
$ x=$(printf %.2 $x)
$ echo $x
0.80

回答by ghostdog74

use awk

使用 awk

x="0.80"
y="0.70"
result=$(awk -vx=$x -vy=$y 'BEGIN{ print x>=y?1:0}')
if [ "$result" -eq 1 ];then
    echo "x more than y"
fi

回答by Haimei

Here is my simple solution:

这是我的简单解决方案:

 BaseLine=70.0         
 if [ $string \> $BaseLine ]
 then
      echo $string
 else
      echo "TOO SMALL"
 fi
 BaseLine=70.0         
 if [ $string \> $BaseLine ]
 then
      echo $string
 else
      echo "TOO SMALL"
 fi

回答by DigitalRoss

The bash language is best characterized as a full-featured macro processor, as such there is no difference between numbers and strings. The problem is that test(1) works on integers.

bash 语言的最大特点是功能齐全的宏处理器,因此数字和字符串之间没有区别。问题是 test(1) 适用于整数。