linux - Bash中的浮点计算和“第16行:[:缺少']'”如何在输出中带入小数点
问题描述
我编写了这段代码来获取 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
解决方案
当它应该是分号 ( ) 时,您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
推荐阅读
- java - 加载类时未调用静态块
- c++ - 处理可能丢失数据的编译器警告的最优雅方法是什么
- javascript - 无法在 iphone 或 ipad pro ios12 上的 Safari 浏览器中下载 PDF
- java - 我正在尝试使用 setcolumnwidth 在 excel 2010 上设置列宽
- python - 使用 python 3 控制 windows 命令行
- sql-server - 如何在两个游标中管理@@fetchstatus?
- html - 当我到达 HTML 页面的特定部分时如何添加声音
- html - Angular 6,一个带有href的标签,使用#link链接到基本页面而不是当前页面
- javascript - 我可以使用 chrome 扩展程序注入 intercom.io 吗?
- ios - 如何在滚动视图中制作一组按钮?