Skip to content

Euclid algorithm in bash #497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 13, 2018

Conversation

Ariana1729
Copy link
Contributor

umm quite useless but slightly better than writing in bf haha, also displayed nicely on gitbook too:>

@@ -1,4 +1,4 @@
# Thomas Algorithm
s# Thomas Algorithm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you typed an s here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait this file should not be here at all!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops got to undo commits

@@ -111,8 +111,6 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"java"](code/java/thomas.java)
{% sample lang="hs" %}
[import, lang:"haskell"](code/haskell/thomas.hs)
{% sample lang="go" %}
[import, lang:"go"](code/golang/thomas.go)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still removing lines, you shouldn't touch thomas_algorithm.md at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops yeah

@berquist berquist self-assigned this Oct 12, 2018
should i use let or $(()), imo let is nicer to look at but still pretty disgusting
Copy link
Member

@leios leios left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code looks pretty clear, but I'll leave it up for a bit in case anyone else wants to review it. Here's just a quick fix.

book.json Outdated
@@ -163,6 +163,10 @@
{
"lang": "lolcode",
"name": "LOLCODE"
},
{
"lang": "bash",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually make lang the file extension so sh in this case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok

Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First shell script, you brave soul.

  1. It is very easy to mix them, because everyone does it, but don't write sh constructs when there are newer better bash ones.
  2. I'll submit an EditorConfig PR (which everyone should be using), but bash should use spaces for indentation, not tabs, and I prefer 4 rather than 2 spaces.
  3. All shell scripts should have the shebang at the top:
#!/usr/bin/env bash

@@ -0,0 +1,38 @@
abs() {
local ret=$1
if [ $ret -lt 0 ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ ... ] is sh (the original Bourne shell). Please use [[ ... ]] for consistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, you can do even better considering the ((...)) syntax (left as an exercise for the reader.

abs() {
local ret=$1
if [ $ret -lt 0 ]; then
let "ret = $ret * -1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are working with integers, do general arithmetic instead, using more modern Bash:

((ret *= -1))

let "b = $a % $b"
let "a = tmp"
done
echo $a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer printf for function returns. The echos at the end of the script are fine.

echo $a
}

result=$(euclid_mod 143 693)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the other examples for consistency:

result=$(euclid_mod $((64 * 67)) $((64 * 81)))

etc.

@berquist
Copy link
Member

Oh, and
4. rename the extension from *.sh to *.bash. This is another terrible habit that everyone has (including me).

@Ariana1729
Copy link
Contributor Author

all my bash scripts are also .sh haha, ok editing

@berquist
Copy link
Member

Not specific to the review, and I'll document it elsewhere, but

  1. It is good practice to use ShellCheck on all your shell scripts. Because of their nature, linters/static analysis to help find problems or smells are more important for shell scripts than most programming languages.
  2. A good style guide to follow is https://github.com/bahamas10/bash-style-guide.

@leios
Copy link
Member

leios commented Oct 12, 2018

Ok. Yeah, you guys are right. The scripts don't need to be .sh. Sorry for that.

@Ariana1729
Copy link
Contributor Author

oh wow shellcheck seems quite cool, giong to update code based on that

@berquist
Copy link
Member

The most common shellcheck error is about quoted variables. While this is the most important thing for daily scripting, it isn't important here at all, so ignore it.

@berquist berquist added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 12, 2018
@berquist
Copy link
Member

You still need to fix the filename.

@@ -59,6 +59,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
[import:2-17, lang:"emojicode"](code/emojicode/euclidean_algorithm.emojic)
{% sample lang="lolcode" %}
[import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol)
{% sample lang="bash" %}
[import:10-22, lang="bash"](code/bash/euclid.bash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've swapped the subtraction and modulus versions in the text.

#!/usr/bin/env bash
abs() {
local ret=$1
if (( $ret < 0 )); then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for $ in any math expression.

@Ariana1729
Copy link
Contributor Author

Ok should be fixed

@berquist
Copy link
Member

I can't pick it apart further, thanks!

@berquist berquist merged commit 594b016 into algorithm-archivists:master Oct 13, 2018
@Ariana1729 Ariana1729 deleted the euclidalgo_sh branch October 13, 2018 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants