shell浮點數運算問題 - Linux

By Margaret
at 2018-12-03T22:44
at 2018-12-03T22:44
Table of Contents
我用unix 系統
寫sh時,遇到了一個bug ,就是浮點數無法運算
我使用ntpdate去校正時間, 並且記錄校正了多少時間
例如:需要校正的時間是 0.000304 s
我寫
ntpdate -p 10.37.82.23 > time.txt
NTP= grep delay time.txt | awk ' ' {print $6}
# $NTP will show 類似 0.000304
if [ $NTP -le 0.100001 ];then
ntpdate -u 10.37.82.23
fi
# 如果NTP server 連線異常, $NTP will show 0.000000
if [ $NTP -eq 0.000000 ];then
echo "NTP server not connect"
fi
上面第一個if 會 不成立 , 可能類似浮點數運算, 他以為兩個都是0
第二個if 也會顯示錯誤, [0.000000 -eq 0.000000 ] 他竟然顯示不相等
請問這是什麼原因,我可以怎麼修正
--
Tags:
Linux
All Comments

By Hedwig
at 2018-12-05T12:18
at 2018-12-05T12:18

By Victoria
at 2018-12-09T22:52
at 2018-12-09T22:52

By Caroline
at 2018-12-11T20:09
at 2018-12-11T20:09

By Blanche
at 2018-12-14T04:47
at 2018-12-14T04:47

By William
at 2018-12-14T11:45
at 2018-12-14T11:45

By Daniel
at 2018-12-19T02:53
at 2018-12-19T02:53

By Mary
at 2018-12-19T07:05
at 2018-12-19T07:05
Related Posts
常常系統變成唯讀狀態,必須要執行fsck才正常

By Damian
at 2018-11-28T23:11
at 2018-11-28T23:11
ubuntu 18.10 vsftpd 架設

By Anthony
at 2018-11-28T10:14
at 2018-11-28T10:14
Debian接FC找不到LUN

By Carol
at 2018-11-28T09:31
at 2018-11-28T09:31
安裝ubuntu 18.04出現Squashfs Errors

By Necoo
at 2018-11-28T05:39
at 2018-11-28T05:39
apache的網頁連不上

By Steve
at 2018-11-27T20:18
at 2018-11-27T20:18