我想写一个计算风向修正角的bash脚本,但每当我运行脚本的trig部分时,就会遇到一个错误。
line 32: sin(-14): syntax error in expression (error token is "(-14)"
我知道WCA的完整公式还没有在那里,但我想在继续之前解决三角函数的问题。脚本贴在下面。如果有任何其他细节需要,我很乐意提供。欢呼吧
#!/bin/bash
# This script is to be used in Cross Country calculations
echo 'What is True Course?'
read true_course
echo 'What is True Airspeed?'
read true_airspeed
echo 'What is Wind Direction?'
read wind_direction
echo 'What is Wind Speed?'
read wind_speed
# Formula for WCA is below
# WCA = sin-1 (sin(WD-TC)*WV/TAS)
course_adjustment=$(($wind_direction-$true_course))
course_adjustment2=$(($course_adjustment*$wind_speed/$true_airspeed))
wca=$((sin($course_adjustment2)))
echo 'Course adjustment is' $course_adjustment
echo 'Course adjustment2 is' $course_adjustment2
# Formula for GS
# GS = SqRt(TAS2 + WV2 - 2*TAS*WV*cos(WD-TC-WCA))
echo 'Calculations complete!'
echo 'True Course is' $true_course
echo 'True Airspeed is' $true_airspeed
echo 'Wind Direction is' $wind_direction
echo 'Wind Speed is' $wind_speed
Bash的算术支持仅限于整数所以你不能在纯Bash中进行三角函数运算.
此外, Bash的算术上下文不支持任何函数的概念, 更不用说三角函数了, 这就是为什么你看到那个听起来很奇怪的错误. $((sin($course_adjustment2)))
相当于 $(($sin($course_adjustment2)))
(算术上下文中的变量不需要一个 $
所以它在寻找一个 $sin
变量)。)
在Bash中做浮点运算的一般方法(除了 完全不用Bash,为什么不是Python?)是与 bc
,如肖恩建议。
# note the -l argument; s[ine] and c[osine] arguments are in radians
$ bc -l <<<"scale=4; s(1); c(1)"
.8414
.5404