【问题标题】:Floating point calculation in Bash and "line 16: [: missing ']'" how to bring decimal point on outputBash中的浮点计算和“第16行:[:缺少']'”如何在输出中带入小数点
【发布时间】:2019-11-26 07:43:47
【问题描述】:

我编写了这段代码来获取 bash 中的浮点温度。这给了我一个错误:

line 16: [: missing ']'

代码如下:

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ]:
do
  let "val = ($counter * 9/5) + 32"
  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1

【问题讨论】:

标签: linux bash shell scripting


【解决方案1】:

您的while 使用冒号 (:),而它应该是分号 (;):

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ];
do
  let "val = ($counter * 9/5) + 32"
  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1

输出:

$ /usr/bin/diff old.sh new.sh 
5c5
< while [ $counter -le 25 ]:
---
> while [ $counter -le 25 ];

$ sh old.sh 
Celsius         Fahrenheit
--------------------------
old.sh: line 5: [: missing `]'

$ sh new.sh 
Celsius         Fahrenheit
--------------------------
0               32
1               33
2               35
3               37
4               39
5               41
6               42
7               44
8               46
9               48
10              50
11              51
12              53
13              55
14              57
15              59
16              60
17              62
18              64
19              66
20              68
21              69
22              71
23              73
24              75
25              77

【讨论】:

  • 第 16 行是 if [[ "$REPLY" ...
  • 当我复制/粘贴 OP 的代码时,我的错误出现在第 5 行,而不是第 16 行——所以 OP 需要提供正确的示例或纠正错误
  • 谢谢,但还是有问题,我没有得到小数点的结果?例如 77.0 或 60.0
  • 部分问题是你从来没有定义REPLY——你能用你尝试过的来更新你的例子吗?
  • 另外,你应该在某处使用bcstackoverflow.com/questions/12722095/…
【解决方案2】:

使用 bc 将给出浮点结果

val=bc &lt;&lt;&lt;"scale=2;($counter*9/5)+32"

#!/bin/bash
echo "Celsius         Fahrenheit"
echo "--------------------------"
counter=0
while [ $counter -le 25 ];
do
 val=`bc  <<<"scale=2;($counter*9/5)+32"`

  if [ $counter -le 9 ]
  then
    whitespace="               "
  else
    whitespace="              "
  fi
  echo "$counter$whitespace$val"
  ((counter++))
if [[ "$REPLY" =~ ^-?[[:digit:]]*\.[[:digit:]]+$ ]]; then
   echo "'$REPLY' is a floating point number."
fi

done
exit 1

答案是

Celsius         Fahrenheit
--------------------------
0               32.00
1               33.80
2               35.60
3               37.40
4               39.20
5               41.00
6               42.80
7               44.60
8               46.40
9               48.20
10              50.00
11              51.80
12              53.60
13              55.40
14              57.20
15              59.00
16              60.80
17              62.60
18              64.40
19              66.20
20              68.00
21              69.80
22              71.60
23              73.40
24              75.20
25              77.00

如果它有助于找到正确的答案

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多