diff --git a/bin/lint b/bin/lint index 716df972..372d9e4a 100755 --- a/bin/lint +++ b/bin/lint @@ -32,4 +32,4 @@ for exercise in "${exercises[@]}"; do done done -shellcheck "${files[@]}" +shellcheck "$@" "${files[@]}" diff --git a/exercises/practice/bottle-song/.meta/example.sh b/exercises/practice/bottle-song/.meta/example.sh index 77a412ad..697744bc 100644 --- a/exercises/practice/bottle-song/.meta/example.sh +++ b/exercises/practice/bottle-song/.meta/example.sh @@ -16,8 +16,9 @@ main() { } verse() { - local cur=$(bottles "$1") - local next=$(bottles "$(( $1 - 1 ))") + local cur next + cur=$(bottles "$1") + next=$(bottles "$(( $1 - 1 ))") printf "%s hanging on the wall,\n" "${cur^}" printf "%s hanging on the wall,\n" "${cur^}" printf "And if one green bottle should accidentally fall,\n" diff --git a/exercises/practice/hamming/.meta/example.sh b/exercises/practice/hamming/.meta/example.sh index 8d7c22b0..afe53844 100644 --- a/exercises/practice/hamming/.meta/example.sh +++ b/exercises/practice/hamming/.meta/example.sh @@ -5,7 +5,7 @@ die() { exit 1 } -if [ "$#" -ne 2 ]; then +if (( $# != 2 )); then die "Usage: hamming.sh " fi diff --git a/exercises/practice/isbn-verifier/.meta/example.sh b/exercises/practice/isbn-verifier/.meta/example.sh index c390f88a..aca4e670 100644 --- a/exercises/practice/isbn-verifier/.meta/example.sh +++ b/exercises/practice/isbn-verifier/.meta/example.sh @@ -25,6 +25,6 @@ done <<< "$number" (( sum += last )) # process the check digit # true if divisible by 11 -[[ $(( sum % 11 )) -eq 0 ]] && printf "%s\n" "true" && exit 0 +(( sum % 11 == 0 )) && printf "%s\n" "true" && exit 0 printf "%s\n" "false" diff --git a/exercises/practice/perfect-numbers/.meta/example.sh b/exercises/practice/perfect-numbers/.meta/example.sh index 03e8f271..c6addc61 100644 --- a/exercises/practice/perfect-numbers/.meta/example.sh +++ b/exercises/practice/perfect-numbers/.meta/example.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +declare -i num sum + aliquot_sum() { local -i num=$1 sum @@ -26,14 +28,14 @@ aliquot_sum() { echo $sum } -declare -i num=$1 +num=$1 if ((num <= 0)); then echo "Classification is only possible for positive integers." >&2 exit 1 fi -declare -i sum=$(aliquot_sum $num) +sum=$(aliquot_sum "$num") if ((sum < num)); then echo deficient elif ((sum > num)); then echo abundant diff --git a/exercises/practice/phone-number/.meta/example.sh b/exercises/practice/phone-number/.meta/example.sh index 9d4ffc75..4bda3896 100644 --- a/exercises/practice/phone-number/.meta/example.sh +++ b/exercises/practice/phone-number/.meta/example.sh @@ -7,7 +7,7 @@ usage() { # echo ": [1]NXX-NXX-XXXX : N=[2-9], X=[0-9]" } -if [ "$#" -ne 1 ]; then +if (( "$#" != 1 )); then usage exit 1 fi @@ -24,4 +24,4 @@ if [[ ! $result =~ $correct_pattern ]]; then fi # Strip off leading 1 country code if exists -echo ${result: -10} +echo "${result: -10}" diff --git a/exercises/practice/prime-factors/.meta/example.sh b/exercises/practice/prime-factors/.meta/example.sh index 8ec26962..33783854 100644 --- a/exercises/practice/prime-factors/.meta/example.sh +++ b/exercises/practice/prime-factors/.meta/example.sh @@ -6,13 +6,13 @@ p=2 while (( p * p <= n )); do if (( n % p == 0 )); then - factors+=( $p ) + factors+=( "$p" ) (( n /= p )) else (( p += 1 )) fi done -(( n > 1 )) && factors+=( $n ) +(( n > 1 )) && factors+=( "$n" ) echo "${factors[*]}" diff --git a/exercises/practice/protein-translation/.meta/example.sh b/exercises/practice/protein-translation/.meta/example.sh index 229753e6..b1fbbe39 100644 --- a/exercises/practice/protein-translation/.meta/example.sh +++ b/exercises/practice/protein-translation/.meta/example.sh @@ -24,7 +24,7 @@ while [[ -n $codons ]]; do exit 1 fi [[ $protein == STOP ]] && break - proteins+=($protein) + proteins+=("$protein") codons=${codons#???} done diff --git a/exercises/practice/queen-attack/.meta/example.sh b/exercises/practice/queen-attack/.meta/example.sh index 91a5d55f..f0b11e43 100644 --- a/exercises/practice/queen-attack/.meta/example.sh +++ b/exercises/practice/queen-attack/.meta/example.sh @@ -16,8 +16,9 @@ main() { validate black $b_row $b_col (( w_row == b_row && w_col == b_col )) && die "cannot occupy same position" - local -i d_row=$( abs $(( w_row - b_row )) ) - local -i d_col=$( abs $(( w_col - b_col )) ) + local -i d_row d_col + d_row=$( abs $(( w_row - b_row )) ) + d_col=$( abs $(( w_col - b_col )) ) if (( d_row == 0 || d_col == 0 || d_row == d_col )) then echo true diff --git a/exercises/practice/rail-fence-cipher/.meta/example.sh b/exercises/practice/rail-fence-cipher/.meta/example.sh index 2f822e71..faf89568 100644 --- a/exercises/practice/rail-fence-cipher/.meta/example.sh +++ b/exercises/practice/rail-fence-cipher/.meta/example.sh @@ -28,7 +28,7 @@ main() { [[ -z $func ]] && die "specify -e or -d" (( rails <= 0 )) && die "must have positive number of rails" shift $((OPTIND - 1)) - $func $rails "$1" + "$func" "$rails" "$1" } encode() { diff --git a/exercises/practice/raindrops/.meta/example.sh b/exercises/practice/raindrops/.meta/example.sh index ace91bf3..ae4047ea 100644 --- a/exercises/practice/raindrops/.meta/example.sh +++ b/exercises/practice/raindrops/.meta/example.sh @@ -6,7 +6,7 @@ usage() { } # check argument is present -if [ ! "$#" -eq 1 ]; then +if (( $# != 1 )); then usage fi @@ -17,20 +17,20 @@ fi output="" -if [ $(($1 % 3)) -eq 0 ]; then +if (( $1 % 3 == 0 )); then output="Pling" fi -if [ $(($1 % 5)) -eq 0 ]; then +if (( $1 % 5 == 0 )); then output=$output"Plang" fi -if [ $(($1 % 7)) -eq 0 ]; then +if (( $1 % 7 == 0 )); then output=$output"Plong" fi -if [ -z $output ]; then - echo $1 +if [[ -z $output ]]; then + echo "$1" else - echo $output + echo "$output" fi diff --git a/exercises/practice/rna-transcription/.meta/example.sh b/exercises/practice/rna-transcription/.meta/example.sh index 911f01bc..dd2bfd6f 100644 --- a/exercises/practice/rna-transcription/.meta/example.sh +++ b/exercises/practice/rna-transcription/.meta/example.sh @@ -6,11 +6,11 @@ trans[C]='G' trans[A]='U' trans[T]='A' -for i in $(seq 1 ${#1}); do +for (( i = 1; i <= ${#1}; i++ )); do nt="${1:$i-1:1}" comp_nt="${trans[$nt]}" - if [ -z "$comp_nt" ]; then + if [[ -z "$comp_nt" ]]; then echo 'Invalid nucleotide detected.' exit 1 fi @@ -18,5 +18,4 @@ for i in $(seq 1 ${#1}); do complement="$complement$comp_nt" done -echo $complement - +echo "$complement" diff --git a/exercises/practice/sieve/.meta/example.sh b/exercises/practice/sieve/.meta/example.sh index aec186ec..864f9569 100644 --- a/exercises/practice/sieve/.meta/example.sh +++ b/exercises/practice/sieve/.meta/example.sh @@ -23,8 +23,8 @@ done primes=() for ((p = 2; p <= limit; p++)); do - if ${is_prime[p]}; then - primes+=( $p ) + if "${is_prime[p]}"; then + primes+=( "$p" ) fi done diff --git a/exercises/practice/triangle/.meta/example.sh b/exercises/practice/triangle/.meta/example.sh index d757a1fd..20f0f6d4 100644 --- a/exercises/practice/triangle/.meta/example.sh +++ b/exercises/practice/triangle/.meta/example.sh @@ -18,13 +18,13 @@ assert() { # Takes a numerical inequality in a string. # Pipes it through to bc # Float-proof numeric comparison : - [[ $( echo $1 | bc -l ) -eq 1 ]] + (( "$( bc -l <<< "$1" )" == 1 )) } output() { exit_code=$1 - if [ $exit_code -eq 0 ]; then + if (( exit_code == 0 )); then echo "true" else echo "false" @@ -43,7 +43,7 @@ valid_triangle() { return 0 } -if ! valid_triangle $s1 $s2 $s3; then +if ! valid_triangle "$s1" "$s2" "$s3"; then # Sides do not meet triangle inequality requirement # Given a <= b <= c and a, b, c != 0, a + b >= c output 1 @@ -52,19 +52,19 @@ fi equilateral() { assert "$1 == $2" && assert "$1 == $3" - output $? + output "$?" } isosceles() { assert "$1 == $2" || assert "$1 == $3" || assert "$2 == $3" - output $? + output "$?" } scalene() { assert "$1 != $2" && assert "$1 != $3" && assert "$2 != $3" - output $? + output "$?" } # Bash Ternary Operator: @@ -73,4 +73,4 @@ scalene() { # If boolean clause is false, it doesn't evaluate the other side of && # If first thing is false, it evaluates the item after ||, # but if first two are true, doesn't bother evaluating last part. -$triangle_type $s1 $s2 $s3 && exit 0 || exit 1 +"$triangle_type" "$s1" "$s2" "$s3" && exit 0 || exit 1