我正在尝试编写一个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发布于 2020-05-11 15:53:29
Bash's arithmetic support is limited to integers,所以你不能在纯Bash中做三角运算。
此外,Bash的算术上下文不支持任何函数概念,更不用说trig函数了,这就是为什么您会看到听起来奇怪的错误。$((sin($course_adjustment2)))等同于$(($sin($course_adjustment2))) (算术上下文中的变量不需要$,所以它寻找$sin变量)。
在Bash中进行浮点运算的一般方法(除了not using Bash at all,为什么不是Python呢?)就像肖恩建议的那样,和bc在一起。
# note the -l argument; s[ine] and c[osine] arguments are in radians
$ bc -l <<<"scale=4; s(1); c(1)"
.8414
.5404https://stackoverflow.com/questions/61723795
复制相似问题