Writing haversine function in bash
I'm trying to convert the python haversine function [here:](https://stackoverflow.com/a/4913653)
def haversine(lon1, lat1, lon2, lat2):
# Calculate the great circle distance in kilometers between two points
# on the earth (specified in decimal degrees)
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.
return c * r
To pure bash. I know, I know, why? Just because really. Anyway I got this:
#!/bin/bash
# Haversine function implementation
# Usage: haversine lat1 lon1 lat2 lon2
# Convert degrees to radians
deg2rad() {
echo $(echo "scale=10; $1 * 0.017453292519943295" | bc -l)
}
# bc has arctan, but not arcsin so, stolen from http://advantage-bash.blogspot.com/2012/12/trignometry-calculator.html
asin() {
if (( $(echo "$1 == 1" | bc -l) ));then
echo "90"
elif (( $(echo "$1 < 1" | bc -l) ));then
echo "scale=3;a(sqrt((1/(1-($1^2)))-1))" | bc -l
elif (( $(echo "$1 > 1" | bc -l) ));then
echo "error"
fi
}
# Haversine formula
haversine() {
R=6371 # Earth radius in km
dLat=$(echo "$3 - $1" | bc -l)
dLon=$(echo "$4 - $2" | bc -l)
lat1=$(deg2rad $1)
lon1=$(deg2rad $2)
lat2=$(deg2rad $3)
lon2=$(deg2rad $4)
a=$(echo "scale=10; (s($dLat/2))^2 + c($lat1) * c($lat2) * (s($dLon/2))^2" | bc -l)
c=$(echo "scale=10; 2 * asin(sqrt($a))" | bc -l)
d=$(echo "scale=10; $R * $c" | bc -l)
echo $d
}
haversine $1 $2 $3 $4
Just some math. The problem I have when I run it with something like `haversine 22.70416 -118.29725 19.57834 -155.07947` I get an error that tells me `asin not defined` right near the end. It's defined right there, right above the haversine function. I'm not sure what it is I'm missing.
https://redd.it/125y1yk
@r_bash
I'm trying to convert the python haversine function [here:](https://stackoverflow.com/a/4913653)
def haversine(lon1, lat1, lon2, lat2):
# Calculate the great circle distance in kilometers between two points
# on the earth (specified in decimal degrees)
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.
return c * r
To pure bash. I know, I know, why? Just because really. Anyway I got this:
#!/bin/bash
# Haversine function implementation
# Usage: haversine lat1 lon1 lat2 lon2
# Convert degrees to radians
deg2rad() {
echo $(echo "scale=10; $1 * 0.017453292519943295" | bc -l)
}
# bc has arctan, but not arcsin so, stolen from http://advantage-bash.blogspot.com/2012/12/trignometry-calculator.html
asin() {
if (( $(echo "$1 == 1" | bc -l) ));then
echo "90"
elif (( $(echo "$1 < 1" | bc -l) ));then
echo "scale=3;a(sqrt((1/(1-($1^2)))-1))" | bc -l
elif (( $(echo "$1 > 1" | bc -l) ));then
echo "error"
fi
}
# Haversine formula
haversine() {
R=6371 # Earth radius in km
dLat=$(echo "$3 - $1" | bc -l)
dLon=$(echo "$4 - $2" | bc -l)
lat1=$(deg2rad $1)
lon1=$(deg2rad $2)
lat2=$(deg2rad $3)
lon2=$(deg2rad $4)
a=$(echo "scale=10; (s($dLat/2))^2 + c($lat1) * c($lat2) * (s($dLon/2))^2" | bc -l)
c=$(echo "scale=10; 2 * asin(sqrt($a))" | bc -l)
d=$(echo "scale=10; $R * $c" | bc -l)
echo $d
}
haversine $1 $2 $3 $4
Just some math. The problem I have when I run it with something like `haversine 22.70416 -118.29725 19.57834 -155.07947` I get an error that tells me `asin not defined` right near the end. It's defined right there, right above the haversine function. I'm not sure what it is I'm missing.
https://redd.it/125y1yk
@r_bash
Stack Overflow
Haversine formula in Python (bearing and distance between two GPS points)
Problem
I would like to know how to get the distance and bearing between two GPS points.
I have researched on the haversine distance. Someone told me that I could also find the bearing using the same
I would like to know how to get the distance and bearing between two GPS points.
I have researched on the haversine distance. Someone told me that I could also find the bearing using the same
program to print out biggest number from a set of numbers
i am trying to make a program to print out like:
How many numbers: 5
1
2
3
4
5
Largest number is 5
​
So my question is, do I need to use like an array method for the set of numbers
https://redd.it/1260aue
@r_bash
i am trying to make a program to print out like:
How many numbers: 5
1
2
3
4
5
Largest number is 5
​
So my question is, do I need to use like an array method for the set of numbers
https://redd.it/1260aue
@r_bash
Reddit
r/bash on Reddit: program to print out biggest number from a set of numbers
Posted by u/RoseLolxd - No votes and 2 comments
How to check in bash if python noscript is running?
I use android (termux) scheduler to trigger following scheduler.sh
#!/data/data/com.termux/files/usr/bin/bash
# load EVs
source "$HOME/storage/shared/Books/AppsCfg/Termux/envfile"
python "$MEMS/Binv/noscripts/binvscheduler.py"
Most of the time it works great (according to schedule android triggers scheduler.sh which triggers binvscheduler.py noscript), but sometimes on device wake up binvscheduler.py gets double triggered. I added create lock file logic into python noscript and it catches 99% of double triggers except one case... (i can't even understand how it's possible. maybe somehow 2 binvscheduler.py run exactly same moment so python can't create lock file fast enough. Not sure)
Anyway, how do i check inside bash (in scheduler.sh) if binvscheduler.py is already running or not?
https://redd.it/12625u0
@r_bash
I use android (termux) scheduler to trigger following scheduler.sh
#!/data/data/com.termux/files/usr/bin/bash
# load EVs
source "$HOME/storage/shared/Books/AppsCfg/Termux/envfile"
python "$MEMS/Binv/noscripts/binvscheduler.py"
Most of the time it works great (according to schedule android triggers scheduler.sh which triggers binvscheduler.py noscript), but sometimes on device wake up binvscheduler.py gets double triggered. I added create lock file logic into python noscript and it catches 99% of double triggers except one case... (i can't even understand how it's possible. maybe somehow 2 binvscheduler.py run exactly same moment so python can't create lock file fast enough. Not sure)
Anyway, how do i check inside bash (in scheduler.sh) if binvscheduler.py is already running or not?
https://redd.it/12625u0
@r_bash
Reddit
r/bash on Reddit: How to check in bash if python noscript is running?
Posted by u/rtwyyn - No votes and 1 comment
What is the meaning of ! -d
I'm sure this has been asked else where so sorry if so. But i'm with a collogue and we can't figure out what this is. Would any one happen to know Really appreciate ti!
https://redd.it/1263vrz
@r_bash
I'm sure this has been asked else where so sorry if so. But i'm with a collogue and we can't figure out what this is. Would any one happen to know Really appreciate ti!
https://redd.it/1263vrz
@r_bash
Reddit
r/bash on Reddit: What is the meaning of ! -d
Posted by u/hometechfan - No votes and 1 comment
I couldn't find a way to check the difference between the current git branch and its parent so I decided to DIY
Hey everyone! As the noscript suggests, I created a small noscript to help me check the difference between my current branch and its parent by keeping a table that points to the parents. here is my repo
https://github.com/A-Siam/diffwatch
I am by no means a noscripting wizard so any suggestions or contribution is much appreciated.
Also, I don't know if my noscript is even a good idea :D if there is any good alternative to my hacky noscript please let me know.
cheers.
https://redd.it/1266mmd
@r_bash
Hey everyone! As the noscript suggests, I created a small noscript to help me check the difference between my current branch and its parent by keeping a table that points to the parents. here is my repo
https://github.com/A-Siam/diffwatch
I am by no means a noscripting wizard so any suggestions or contribution is much appreciated.
Also, I don't know if my noscript is even a good idea :D if there is any good alternative to my hacky noscript please let me know.
cheers.
https://redd.it/1266mmd
@r_bash
GitHub
GitHub - A-Siam/diffwatch: A small noscript to keep PRs small and sweet... or maybe a noscript that annoys you by showing how far you…
A small noscript to keep PRs small and sweet... or maybe a noscript that annoys you by showing how far you went from your ancestors - GitHub - A-Siam/diffwatch: A small noscript to keep PRs small and sw...
How to delete a matching line and it's line break in a file
I have a .conf file where I use echo to append a line of text with a line break. But if I later want to remove the line I can't remove it and the line break.
To append the line to the end of file I use:
echo 'drive_db_test_url="127.0.0.1"' >> "$file"
Then I can remove the line with the following but it leaves the line break:
sed -i 's/drive_db_test_url=\"127\.0\.0\.1\"//' "$file"
I've tried things like but they don't work:
sed -irz 's/drive_db_test_url=\"127\.0\.0\.1\"\n//' "$file"
And I've seen all sorts of ways to remove all line breaks in a file, using tr, sed or awk but I don't want to remove all line breaks.
https://redd.it/126au5r
@r_bash
I have a .conf file where I use echo to append a line of text with a line break. But if I later want to remove the line I can't remove it and the line break.
To append the line to the end of file I use:
echo 'drive_db_test_url="127.0.0.1"' >> "$file"
Then I can remove the line with the following but it leaves the line break:
sed -i 's/drive_db_test_url=\"127\.0\.0\.1\"//' "$file"
I've tried things like but they don't work:
sed -irz 's/drive_db_test_url=\"127\.0\.0\.1\"\n//' "$file"
And I've seen all sorts of ways to remove all line breaks in a file, using tr, sed or awk but I don't want to remove all line breaks.
https://redd.it/126au5r
@r_bash
Reddit
r/bash on Reddit: How to delete a matching line and it's line break in a file
Posted by u/DaveR007 - No votes and 1 comment
2>&1
You know what gets my head in?
In Linux/Bash when we want something to run in the background we would do something like this: ./yourCommand > /dev/null 2>&1 &
I am taking issue with 2>&1. "&1" refers to a variable as we can see by the "&", the standard bash variable "standard output" so far so good :> all makes sense.
But the "2" here refers to another variable "standard error" why then we don't write &2>&1?
Would ./yourCommand > /dev/null 2>1 & be valid syntax and work?
https://redd.it/126ogzm
@r_bash
You know what gets my head in?
In Linux/Bash when we want something to run in the background we would do something like this: ./yourCommand > /dev/null 2>&1 &
I am taking issue with 2>&1. "&1" refers to a variable as we can see by the "&", the standard bash variable "standard output" so far so good :> all makes sense.
But the "2" here refers to another variable "standard error" why then we don't write &2>&1?
Would ./yourCommand > /dev/null 2>1 & be valid syntax and work?
https://redd.it/126ogzm
@r_bash
Reddit
r/bash on Reddit: 2>&1
Posted by u/arvamircea - No votes and 11 comments
I made simple IPTV player in bash
https://github.com/shahin8r/iptv
With support for your own M3U playlists.
​
https://i.redd.it/6pcs7dughwqa1.gif
https://redd.it/126qzeh
@r_bash
https://github.com/shahin8r/iptv
With support for your own M3U playlists.
​
https://i.redd.it/6pcs7dughwqa1.gif
https://redd.it/126qzeh
@r_bash
GitHub
GitHub - shahin8r/iptv: A simple CLI IPTV player for M3U playlists with fuzzy finding in the terminal.
A simple CLI IPTV player for M3U playlists with fuzzy finding in the terminal. - shahin8r/iptv
A helper function to tell if a noscript is sourced?
I write a fair amount of noscripts and I often have strong opinions on whether those noscripts should be sourced or executed. It's usually because I don't want to screw over future me because I've written a noscript that is a little aggressive with the environment.
For those noscripts I have a block of code I run:
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi
I also hate copy and pasted code, but because this relies on the behavior return has depending on if a noscript is sourced or executed, I can't really put this is a helper function and call that helper function. Instead I do this (mildly crazy) thing where I have a bunch of noscripts stored in variables and then I just eval those in the noscript.
So I have a utiltiy noscript with something like:
# shellcheck disable=SC2034
BASH_COMMON_SCRIPT_UNSOURCEABLE=$(cat <<'EOM'
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi
EOM
)
And then I use this in a noscript kinda like:
CONFIGROOT_DIR_SCRIPT="$( cd "$( dirname "$( readlink -f "${BASH_SOURCE[0]}" )" )" >/dev/null 2>&1 && git rev-parse --show-toplevel 2>/dev/null )"
if [ -f "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh" ]; then
# shellcheck disable=SC1091
source "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh"
fi
eval "${BASH_COMMON_SCRIPT_UNSOURCEABLE}"
And yes I'm aware of the perils of eval'ing an env var.
Is it possible to do this in a more sensible way?
https://redd.it/126tv9y
@r_bash
I write a fair amount of noscripts and I often have strong opinions on whether those noscripts should be sourced or executed. It's usually because I don't want to screw over future me because I've written a noscript that is a little aggressive with the environment.
For those noscripts I have a block of code I run:
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi
I also hate copy and pasted code, but because this relies on the behavior return has depending on if a noscript is sourced or executed, I can't really put this is a helper function and call that helper function. Instead I do this (mildly crazy) thing where I have a bunch of noscripts stored in variables and then I just eval those in the noscript.
So I have a utiltiy noscript with something like:
# shellcheck disable=SC2034
BASH_COMMON_SCRIPT_UNSOURCEABLE=$(cat <<'EOM'
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi
EOM
)
And then I use this in a noscript kinda like:
CONFIGROOT_DIR_SCRIPT="$( cd "$( dirname "$( readlink -f "${BASH_SOURCE[0]}" )" )" >/dev/null 2>&1 && git rev-parse --show-toplevel 2>/dev/null )"
if [ -f "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh" ]; then
# shellcheck disable=SC1091
source "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh"
fi
eval "${BASH_COMMON_SCRIPT_UNSOURCEABLE}"
And yes I'm aware of the perils of eval'ing an env var.
Is it possible to do this in a more sensible way?
https://redd.it/126tv9y
@r_bash
Reddit
r/bash on Reddit: A helper function to tell if a noscript is sourced?
Posted by u/NerdyCrimeFighter - No votes and no comments
CSV to variables
I'm trying to parse a csv to variables to use it in "update all wordpress/nextcloud/..." noscripts
The CSV looks like this:
nicename,application,directory
blog1,wordpress,/var/www/wordpress1
blog2,wordpress,/var/www/wordpress2
cloud,nextcloud,/var/www/nextcloud
How do i parse these into variables like $blog1.directory (using the nicename)?
https://redd.it/126rxvi
@r_bash
I'm trying to parse a csv to variables to use it in "update all wordpress/nextcloud/..." noscripts
The CSV looks like this:
nicename,application,directory
blog1,wordpress,/var/www/wordpress1
blog2,wordpress,/var/www/wordpress2
cloud,nextcloud,/var/www/nextcloud
How do i parse these into variables like $blog1.directory (using the nicename)?
https://redd.it/126rxvi
@r_bash
Need help creating a simple loading noscript
Hi everyone! I am currently creating a noscript that saves data to a file, and another one that loads that data. I have already create the saving part, but now I need to figure out the loading part. I need to load the even lines as "arg1(number of line)" and "arg2(number of line)". How can i do it?
https://redd.it/126xl55
@r_bash
Hi everyone! I am currently creating a noscript that saves data to a file, and another one that loads that data. I have already create the saving part, but now I need to figure out the loading part. I need to load the even lines as "arg1(number of line)" and "arg2(number of line)". How can i do it?
https://redd.it/126xl55
@r_bash
Reddit
r/bash on Reddit: Need help creating a simple loading noscript
Posted by u/imjusthereforthelul - No votes and no comments
Aiyu, a set of shell functions that wraps around all major AI projects and interconnects them. It's a shell function!
https://github.com/GabrieleRisso/aiyu
https:\/\/github.com\/GabrieleRisso\/aiyu
https://redd.it/126xde6
@r_bash
https://github.com/GabrieleRisso/aiyu
https:\/\/github.com\/GabrieleRisso\/aiyu
https://redd.it/126xde6
@r_bash
GitHub
GitHub - GabrieleRisso/aiyu: shell functions building blocks for advance ai.
shell functions building blocks for advance ai. . Contribute to GabrieleRisso/aiyu development by creating an account on GitHub.
Learning bash is so frustrating
Can you recommend to me any site more preferably a pdf tutorial for bash.
Thank you.
https://redd.it/1272uv3
@r_bash
Can you recommend to me any site more preferably a pdf tutorial for bash.
Thank you.
https://redd.it/1272uv3
@r_bash
Reddit
r/bash on Reddit: Learning bash is so frustrating
Posted by u/SuggestionFit5492 - No votes and 2 comments
Color schemes in Micro
Hello, I am just wondering how to change the color scheme of micro text editor in a bash noscript.
the command to change the color scheme is
^e set colorscheme "colorscheme"
https://redd.it/1279ifv
@r_bash
Hello, I am just wondering how to change the color scheme of micro text editor in a bash noscript.
the command to change the color scheme is
^e set colorscheme "colorscheme"
https://redd.it/1279ifv
@r_bash
Reddit
r/bash on Reddit: Color schemes in Micro
Posted by u/04AE - No votes and no comments
Made a noscript manager for non-savvy users
Hey there,
​
I work with a lot of field techs, skill levels on computers vary. We have a bunch of various noscripts that we need the techs to run, but some have a hard time staying organized.
I made this so that there is a single folder, where all the noscripts reside. You only have to open this single "app" if you call it that. From there, they can run multiple noscripts with an interface.
​
I couldn't really find anything like this, so I made it.
​
Thoughts on improving? Also feel free to use it.
​
https://github.com/kylezwhite/slector
https://redd.it/127f4tk
@r_bash
Hey there,
​
I work with a lot of field techs, skill levels on computers vary. We have a bunch of various noscripts that we need the techs to run, but some have a hard time staying organized.
I made this so that there is a single folder, where all the noscripts reside. You only have to open this single "app" if you call it that. From there, they can run multiple noscripts with an interface.
​
I couldn't really find anything like this, so I made it.
​
Thoughts on improving? Also feel free to use it.
​
https://github.com/kylezwhite/slector
https://redd.it/127f4tk
@r_bash
GitHub
GitHub - kylezwhite/slector: Script selector tool
Script selector tool. Contribute to kylezwhite/slector development by creating an account on GitHub.
Sshto update
Hi, sshto got an update, I've added quick filter!)
Due to lack of buttons in dialog I had to add it to the list.
https://preview.redd.it/wz0q5bnq03ra1.png?width=861&format=png&auto=webp&v=enabled&s=beb7d077864954f90e22aab7a4d148e49ae35f4a
https://redd.it/127mqo0
@r_bash
Hi, sshto got an update, I've added quick filter!)
Due to lack of buttons in dialog I had to add it to the list.
https://preview.redd.it/wz0q5bnq03ra1.png?width=861&format=png&auto=webp&v=enabled&s=beb7d077864954f90e22aab7a4d148e49ae35f4a
https://redd.it/127mqo0
@r_bash
GitHub
GitHub - vaniacer/sshto: Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config.…
Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config. It can not only connect but also to run commands, copy files, tunnel ports. - vaniacer/sshto
Question about calls with relative paths
The following is a call with an absolute path specification that works for me. This call, opens the file samplefile with the editor xed:
xed /a/b/c/d/e/f/g/samplefile.txt
Suppose I open a terminal and type the following:
cd /a/b/c/d
How would I then have to open the text file with the editor xed from this position with a relative specification. For example the following call I tried seems to be wrong here:
./e/f/g/samplefile.txt
https://redd.it/12809zu
@r_bash
The following is a call with an absolute path specification that works for me. This call, opens the file samplefile with the editor xed:
xed /a/b/c/d/e/f/g/samplefile.txt
Suppose I open a terminal and type the following:
cd /a/b/c/d
How would I then have to open the text file with the editor xed from this position with a relative specification. For example the following call I tried seems to be wrong here:
./e/f/g/samplefile.txt
https://redd.it/12809zu
@r_bash
Reddit
r/bash on Reddit: Question about calls with relative paths
Posted by u/TitleApprehensive360 - No votes and 1 comment
Issue with sorting grep results in bash noscript
I do not know how to get my bash noscript to produce the right output, but it's very close. I need it to sort from highest frequency codon to lowest like this for an example correct output:
aac 20
aag 9
cgg 2
pastebin w/ code: https://pastebin.com/hH1p1Yxk
output: https://imgur.com/a/y78dy5l
https://redd.it/12854yn
@r_bash
I do not know how to get my bash noscript to produce the right output, but it's very close. I need it to sort from highest frequency codon to lowest like this for an example correct output:
aac 20
aag 9
cgg 2
pastebin w/ code: https://pastebin.com/hH1p1Yxk
output: https://imgur.com/a/y78dy5l
https://redd.it/12854yn
@r_bash
Pastebin
bash_noscript - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
I don't know who needs to see this but I wrote a noscript to optimize Arch for Persian/Farsi writing/reading. (Link below)
Here is the noscript
It's a dead-simple noscript that only installs some packages and generates proper locale and sets the time and date, but I guess it could still help new users.
Cheers.
https://redd.it/1287vum
@r_bash
Here is the noscript
It's a dead-simple noscript that only installs some packages and generates proper locale and sets the time and date, but I guess it could still help new users.
Cheers.
https://redd.it/1287vum
@r_bash
GitHub
GitHub - wolandark/Arch-Persianizer: A simple noscript for localizing Arch linux for RTL and Persian language یک اسکریپت ساده برای…
A simple noscript for localizing Arch linux for RTL and Persian language یک اسکریپت ساده برای فارسیسازی آرچ لینوکس - wolandark/Arch-Persianizer
N00b question
This is the noscript I'm using.
Now, I'm supposed to replace every instance of
https://redd.it/128g3h7
@r_bash
This is the noscript I'm using.
Now, I'm supposed to replace every instance of
RAW_FILE with the actual path to my file. I'm having trouble with this. I've tried single quotes, double quotes, a space between the parentheses, and none of them work. Let's say the file in question is called file.xyz for example, how would I reference it? Note that the file is in the same directory as the noscript.https://redd.it/128g3h7
@r_bash