hello i want to calcul the distance between me and the ISS so this my code
i want to print this sentence :
The ISS is currently located at -30.1461, -50.7975: 10010km from us!
my code :
#!/bin/bash
pi = 3.14
earthRadiumKm=6371
lat2=48.813875
on2=2.392521
com=$( curl -s 'aletum.jails.simplerezo.com/etna-iss.json' | sed s/\"//g | awk\ -v RS =',' -F: '{print $1 $2 $3}' )
lat1=$( echo $com | cut c-60-66)
lon1=$( echo $com | cut c-42-50)
dLat=$( echo "($lat2 - $lat1) * $pi / 180" | bc -l)
dLat=$( echo "($lon2 - $lon1) * $pi / 180" | bc -l)
l1=$( echo "($lat1) * $pi / 180" | bcc -l)
l2=$( echo "($lat2) * $pi / 180" | bcc -l)
a=$( echo "sinus($dLat / 2) * sinus(dLat / 2) + sinus(dLon / 2) * sinus(dLon / 2) * cosine($l1) * consine($l2) | bc -l)
result=$( echo "2 * atan2(sqrt($a), sqrt(1-$a)) * $earthRaduisKm" | bc -1)
echo "The ISS is currently located at $dLat, $dLon, : ${result}KM from us!"
my Problem is my calcul are false because the result echo 0.6 km and that is impossible i thinks it's because i don't know how to use atan2
Fun little project. I have no idea if your math is right, but your code is quite terribly wrong:
no spaces around the = in an assignment: pi = 3.14 => pi=3.14
spelling error for variable names: earthRadiumKm=... => $earthRaduisKm
incorrect bc function names
incorrect system command names: awk\ -v, bcc -l
unclosed quoted string
Refactored:
#!/bin/bash
read lon1 lat1 time < <(
curl -s 'aletum.jails.simplerezo.com/etna-iss.json' |
jq -r '"\(.iss_position.longitude) \(.iss_position.latitude) \(.timestamp)"'
)
lon2=2.392521
lat2=48.813875
{ read dLat; read dLon; read result; } < <(
bc -l <<END
pi = (4*a(1/5) - a(1/239))*4
earthrad = 6371
dlat = ($lat2 - $lat1) * pi/180
dlon = ($lon2 - $lon1) * pi/180
a = s(dlat/2) * s(dlat/2) + s(dlon/2) * s(dlon/2) * c($lat1 * pi/180) * c($lat2 * pi/180)
result = 2 * a( sqrt(a) / sqrt(1-a) ) * earthrad
dlat
dlon
result
END
)
printf "At %s,\n the ISS is currently located at %.4f,%.4f : %.2f KM from us\n" \
"$(date -d "#$time" "+%F %T %Z")" \
"$dLat" "$dLon" "$result"
result
At 2018-09-12 08:53:32 EDT,
the ISS is currently located at 0.5916,-2.2398 : 11296.11 KM from us
Notes:
repeat, I have no idea if your math is right
Process Substitutions to execute some code and read the results into variables.
use jq to parse JSON
I pushed all the math into a single bc call.
bc -l uses s for sine, c for cosine and a for arctan: read the bc man page
bc has no arctan2 function
I use a formula to get pi a little more precise
bc variable names must be all lowercase.
Related
I've downloaded a script from github, which seems to work, but I'm getting the error when running.
https://github.com/chentda/curl-average-timings
The script calculates the average loading time from a URL
#!/bin/bash
#This is a simple bash script to get the averages of various website response timings using cURL
#Formatted cURL output that contains the various response timings separated by '-'
curl_format="%{time_namelookup}-%{time_connect}-%{time_appconnect}-%{time_pretransfer}-%{time_redirect}-%{time_starttransfer}-%{time_total}"
#Website you want to send the request to
URL="https://www.google.com/"
#How many requests you want to hit website with to get averages
iterations=5
decimal_rounding=5
#Initialising total variables for the various timings
total_time_namelookup="0"
total_time_connect="0"
total_time_appconnect="0"
total_time_pretransfer="0"
total_time_redirect="0"
total_time_starttransfer="0"
total_time_total="0"
for i in `seq 1 $iterations`;
do
response=$(curl -o /dev/null -s -w $curl_format $URL)
#Splits response string by the delimiter of "-"
response_times=($(echo "$response" | tr "-" "\n"))
#Assigning each type of response time to a variable
time_namelookup=${response_times[0]}
time_connect=${response_times[1]}
time_appconnect=${response_times[2]}
time_pretransfer=${response_times[3]}
time_redirect=${response_times[4]}
time_starttransfer=${response_times[5]}
time_total=${response_times[6]}
#Adding variables assigned above in loop to the respective total variables that are set at start of script
total_time_namelookup=$(echo "$total_time_namelookup + $time_namelookup" | bc)
total_time_connect=$(echo "$total_time_connect + $time_connect" | bc)
total_time_appconnect=$(echo "$total_time_appconnect + $time_appconnect" | bc)
total_time_pretransfer=$(echo "$total_time_pretransfer + $time_pretransfer" | bc)
total_time_redirect=$(echo "$total_time_redirect + $time_redirect" | bc)
total_time_starttransfer=$(echo "$total_time_starttransfer + $time_starttransfer" | bc)
total_time_total=$(echo "$total_time_total + $time_total" | bc)
done
#Calculating the average for each type of response time
average_time_namelookup=$(echo "scale=$decimal_rounding; $total_time_namelookup / $iterations" | bc)
average_time_connect=$(echo "scale=$decimal_rounding; $total_time_connect / $iterations" | bc)
average_time_appconnect=$(echo "scale=$decimal_rounding; $total_time_appconnect / $iterations" | bc)
average_time_pretransfer=$(echo "scale=$decimal_rounding; $total_time_pretransfer / $iterations" | bc)
average_time_redirect=$(echo "scale=$decimal_rounding; $total_time_redirect / $iterations" | bc)
average_time_starttransfer=$(echo "scale=$decimal_rounding; $total_time_starttransfer / $iterations" | bc)
average_time_total=$(echo "scale=$decimal_rounding; $total_time_total / $iterations" | bc)
echo "Averages of response timings:"
echo ""
echo " time_namelookup: $average_time_namelookup"
echo " time_connect: $average_time_connect"
echo " time_appconnect: $average_time_appconnect"
echo " time_pretransfer: $average_time_pretransfer"
echo " time_redirect: $average_time_redirect"
echo "time_starttransfer: $average_time_starttransfer"
echo " --------"
echo " time_total: $average_time_total"
After a few debuggin, I can see that the problem is on this line, when uses bc to sum results
total_time_namelookup=$(echo "$total_time_namelookup + $time_namelookup" | bc)
But it throws
(standard_in) 1: syntax error
But I can't figure what I'm doing wrong.
I'm on ubuntu running from the terminal with sh
Your numeric values contains a locale specific decimal separator (,). bc only knows .. Convert your string containing numeric values by running them through tr , '.' before sending them to bc.
Or, as suggested by tripleee, set the C locale explicitly at the top of the script:
export LC_ALL=C
For more complex locale operations, you may need library support:
https://github.com/leagris/lcnumconv.sh
"Despite producing floating point results. Bash does not support other type of arguments than integers, so you need to rather invoke external tools like bc for your math or stick to integers only." 4.5: syntax error: invalid arithmetic operator (error token is ".5") - but the code still seems to work, why?
Usually I use the bc external tool to fix it, but now I have a function and I do not know where to use it exactly, can you help me?
#!/bin/bash
function crossProduct {
declare -a v1=("${!1}")
declare -a v2=("${!2}")
#Note: Can't pass by reference, so the global variable must be used
vectResult[0]=$(( (v1[1] * v2[2]) - (v1[2] * v2[1]) ))
vectResult[1]=$(( - ((v1[0] * v2[2]) - (v1[2] * v2[0])) ))
vectResult[2]=$(( (v1[0] * v2[1]) - (v1[1] * v2[0]) ))
}
vect1[0]=0.3
vect1[1]=-0.3
vect1[2]=0.1
vect2[0]=0.4
vect2[1]=0.9
vect2[2]=2.3
crossProduct vect1[#] vect2[#]
echo ${vectResult[0]} ${vectResult[1]} ${vectResult[2]}
You can pass references, namely as local -n arr=$1:
$ function _tmp {
local -n arr=$1
for i in ${arr[#]}; do echo $i; done
}
$ TMP=(1 2 3)
$ _tmp TMP
1
2
3
Now to the bc question; it parses a string and returns the value of it. Therefore you should use it as:
# make sure you declare vectResult first.
function crossProduct {
declare -a v1=("${!1}")
declare -a v2=("${!2}")
vectResult[0]=$( echo "(${v1[1]} * ${v2[2]}) - (${v1[2]} * ${v2[1]}) " | bc)
vectResult[1]=$( echo "- ((${v1[0]} * ${v2[2]}) - (${v1[2]} * ${v2[0]}))" | bc )
vectResult[2]=$( echo "(${v1[0]} * ${v2[1]}) - (${v1[1]} * ${v2[0]})" | bc )
}
Combining the two as how I would implement it:
#!/bin/bash
function crossProduct {
local -n v1=$1
local -n v2=$2
local result=()
# You should remain to use bc because Bash only does integers.
result+=($( echo "(${v1[1]} * ${v2[2]}) - (${v1[2]} * ${v2[1]}) " | bc))
result+=($( echo "-((${v1[0]} * ${v2[2]}) - (${v1[2]} * ${v2[0]}))" | bc ))
result+=($( echo "(${v1[0]} * ${v2[1]}) - (${v1[1]} * ${v2[0]})" | bc ))
echo "${result[#]}"
}
vect1[0]=0.3
vect1[1]=-0.3
vect1[2]=0.1
vect2[0]=0.4
vect2[1]=0.9
vect2[2]=2.3
vectResult=($(crossProduct vect1 vect2))
echo ${vectResult[0]} ${vectResult[1]} ${vectResult[2]}
exit 0
This produces -.6 -.6 .3
I'm having a syntax error ((standard_in): syntax error)on 3rd and 5th line.
#!/bin/bash
i=`echo "8.8007751822"|bc`
rws = `echo "0.49237251092*$i" |bc`
rmt = `echo "0.85 * $rws"| bc`
dx = `echo "log ($rws / 0.000001) / 720.0" | bc`;
Can anyone help me?
A few things:
Assignments must not have blanks around the =
i=`echo "8.8007751822"|bc` is a really complicated way to write i=8.8007751822
bc has no function log, there's only l for the natural logarithm (and l requires the -l option to be enabled)
I would move everything into bc instead of calling it multiple times:
bc -l <<'EOF'
i = 8.8007751822
rws = i * 0.49237251092
rmt = 0.85 * rws
dx = (l(rws / 0.000001) / l(10)) / 720
dx
EOF
This prints the value of dx.
I'm simply trying to multiplying some float variables using bc:
#!/bin/bash
a=2.77 | bc
b=2.0 | bc
for cc in $(seq 0. 0.001 0.02)
do
c=${cc} | bc
d=$((a * b * c)) | bc
echo "$d" | bc
done
And this does not give me an output. I know it's a silly one but I've tried a number of combinations of bc (piping it in different places etc.) to no avail.
Any help would be greatly appreciated!
bc is a command-line utility, not some obscure part of shell syntax. The utility reads mathematical expressions from its standard input and prints values to its standard output. Since it is not part of the shell, it has no access to shell variables.
The shell pipe operator (|) connects the standard output of one shell command to the standard input of another shell command. For example, you could send an expression to bc by using the echo utility on the left-hand side of a pipe:
echo 2+2 | bc
This will print 4, since there is no more here than meets the eye.
So I suppose you wanted to do this:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
echo "$a * $b * $c" | bc
done
Note: The expansion of the shell variables is happening when the shell processes the argument to echo, as you could verify by leaving off the bc:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
echo -n "$a * $b * $c" =
echo "$a * $b * $c" | bc
done
So bc just sees numbers.
If you wanted to save the output of bc in a variable instead of sending it to standard output (i.e. the console), you could do so with normal command substitution syntax:
a=2.77
b=2.0
for c in $(seq 0. 0.001 0.02); do
d=$(echo "$a * $b * $c" | bc)
echo "$d"
done
To multiply two numbers directly, you would do something like:
echo 2.77 * 2.0 | bc
It will produce a result to 2 places - the largest number of places of the factors. To get it to a larger number of places, like 5, would require:
echo "scale = 5; 2.77 * 2.0" | bc
This becomes more important if you're multiplying numerals that each have a large number of decimal places.
As stated in other replies, bc is not a part of bash, but is a command run by bash. So, you're actually sending input directly to the command - which is why you need echo. If you put it in a file (named, say, "a") then you'd run "bc < a". Or, you can put the input directly in the shell script and have a command run the designated segment as its input; like this:
cat <<EOF
Input
EOF
... with qualifiers (e.g. you need to write "" as "\", for instance).
Control flow constructs may be more problematic to run in BC off the command line. I tried the following
echo "scale = 6; a = 2.77; b = 2.0; define f(cc) { auto c, d; c = cc; d = a*b*c; return d; } f(0); f(0.001); f(0.02)" | bc
and got a syntax error (I have a version of GNU-BC installed). On the other hand, it will run fine with C-BC
echo "scale = 6; a = 2.77; b = 2.0; define f(cc) { auto c, d; c = cc; d = a * b * c; return d; } f(0); f(0.001); f(0.02)" | cbc
and give you the expected result - matching the example you cited ... listing numbers to 6 places.
C-BC is here (it's operationally a large superset of GNU-BC and UNIX BC, but not 100% POSIX compliant):
https://github.com/RockBrentwood/CBC
The syntax is closer to C, so you could also write it as
echo "scale = 6, a = 2.77, b = 2.0; define f(cc) { return a * b * cc; } f(0); f(0.001); f(0.02)" | cbc
to get the same result. So, as another example, this
echo "scale = 100; for (x = 0, y = 1; x < 50; y *= ++x); y" | cbc
will give you 50 factorial. However, comma-expressions, like (x = 0, y = 1) are not mandated for bc by POSIX, so it will not run in other bc dialects, like GNU BC.
I know it's really stupid question, but I don't know how to do this in bash:
20 / 30 * 100
It should be 66.67 but expr is saying 0, because it doesn't support float.
What command in Linux can replace expr and do this equalation?
bc will do this for you, but the order is important.
> echo "scale = 2; 20 * 100 / 30" | bc
66.66
> echo "scale = 2; 20 / 30 * 100" | bc
66.00
or, for your specific case:
> export ach_gs=2
> export ach_gs_max=3
> x=$(echo "scale = 2; $ach_gs * 100 / $ach_gs_max" | bc)
> echo $x
66.66
Whatever method you choose, this is ripe for inclusion as a function to make your life easier:
#!/bin/bash
function pct () {
echo "scale = $3; $1 * 100 / $2" | bc
}
x=$(pct 2 3 2) ; echo $x # gives 66.66
x=$(pct 1 6 0) ; echo $x # gives 16
just do it in awk
# awk 'BEGIN{print 20 / 30 * 100}'
66.6667
save it to variable
# result=$(awk 'BEGIN{print 20 / 30 * 100}')
# echo $result
66.6667
I generally use perl:
perl -e 'print 10 / 3'
As reported in the bash man page:
The shell allows arithmetic expressions to be evaluated, under certain circumstances...Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error.
You can multiply by 100 earlier to get a better, partial result:
let j=20*100/30
echo $j
66
Or by a higher multiple of 10, and imagine the decimal place where it belongs:
let j=20*10000/30
echo $j
66666
> echo "20 / 30 * 100" | bc -l
66.66666666666666666600
This is a simplification of the answer by paxdiablo. The -l sets the scale (number of digits after the decimal) to 20. It also loads a math library with trig functions and other things.
Another obvious option:
python -c "print(20 / 30 * 100)"
assuming you are using Python 3. Otherwise, use python3.