I need to learn bash at some point in the near future, so I decided to start playing around with it. This is really the first actual script that I've written and would like feedback.
This finds and prints all the prime numbers up to the argument provided to the script:
./primes.sh 100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Honestly, this is proving to be a challenging language with very finicky syntax and a lot of ways to do things half-incorrectly. This code works, but I'm sure it's far from optimal. I'd like any and all recommendations.
I intentionally included the mostly-unnecessary hasFactor
function just to force use of functions. I wanted to see what complications it caused. The main issue with it is needing to return a number representing a boolean that indicates if the number has a factor or not, and cleanly handling that returned exit code at the "call-site". Needing to check against 1
seems poor. Any suggestions regarding use of functions would be appreciated.
And I know better algorithms exist, but I wanted to keep it simple.
#!/usr/bin/env bash
maxNumber=$1
# Returns if a number has factors other than 1 and itself
# Params: n - number to check
function hasFactor {
n=$1
for ((factor=2; factor<n; factor++)); do
if [ $((n % factor)) -eq 0 ]; then
true
return
fi
done
false
return
}
for ((n=2; n<maxNumber; n++)); do
hasFactor $n
if [ $? = 1 ]; then
echo $n
fi
done