diff --git a/exercises/practice/bowling/.meta/example.sh b/exercises/practice/bowling/.meta/example.sh index cceb0fe4..3da03cc8 100644 --- a/exercises/practice/bowling/.meta/example.sh +++ b/exercises/practice/bowling/.meta/example.sh @@ -11,12 +11,12 @@ main() { (( frame > 10 )) && die "Cannot roll after game is over" (( pins < 0 )) && die "Negative roll is invalid" (( pins > 10 )) && die "Pin count exceeds pins on the lane" - too_many $pins && die "Pin count exceeds pins on the lane" - add_score $pins - handle_frame $pins + too_many "$pins" && die "Pin count exceeds pins on the lane" + add_score "$pins" + handle_frame "$pins" done (( frame <= 10 )) && die "Score cannot be taken until the end of the game" - echo $score + echo "$score" } die() { @@ -32,7 +32,7 @@ too_many() { if (( ${#current_frame[@]} == 0 )); then return 1 elif (( frame == 10 )); then - too_many_tenth $pins + too_many_tenth "$pins" else (( current_frame[0] + pins > 10 )) fi @@ -41,8 +41,8 @@ too_many() { too_many_tenth() { local pins=$1 local non_strikes=() - for roll in ${current_frame[@]}; do - (( roll < 10 )) && non_strikes+=( $roll ) + for roll in "${current_frame[@]}"; do + (( roll < 10 )) && non_strikes+=( "$roll" ) done local num=${#non_strikes[@]} if (( num == 0 )) || @@ -57,7 +57,7 @@ too_many_tenth() { add_score() { local pins=$1 (( score += pins )) - for idx in ${!bonuses[@]}; do + for idx in "${!bonuses[@]}"; do if (( bonuses[idx] > 0 )); then (( score += pins )) (( bonuses[idx] -= 1 )) @@ -67,9 +67,9 @@ add_score() { handle_frame() { if (( frame == 10 )); then - handle_tenth_frame $1 + handle_tenth_frame "$1" else - handle_nth_frame $1 + handle_nth_frame "$1" fi } @@ -84,7 +84,7 @@ handle_nth_frame() { (( frame += 1 )) elif (( ${#current_frame[@]} == 0 )); then # first ball of frame - current_frame+=( $pins ) + current_frame+=( "$pins" ) else # second ball of frame if (( current_frame[0] + pins == 10 )); then @@ -97,7 +97,7 @@ handle_nth_frame() { handle_tenth_frame() { local pins=$1 - current_frame+=( $pins ) + current_frame+=( "$pins" ) local num=${#current_frame[@]} if (( num == 3 )) || (( num == 2 && $(sum current_frame) < 10 )) @@ -110,8 +110,8 @@ sum() { # sum the contents of an array local -n ary=$1 local -i sum=0 - for val in ${ary[@]}; do (( sum += val )); done - echo $sum + for val in "${ary[@]}"; do (( sum += val )); done + echo "$sum" } main "$@" diff --git a/exercises/practice/change/.meta/example.sh b/exercises/practice/change/.meta/example.sh index a3109779..c308a1f2 100644 --- a/exercises/practice/change/.meta/example.sh +++ b/exercises/practice/change/.meta/example.sh @@ -4,7 +4,9 @@ main() { local -i amount=$1; shift + # shellcheck disable=SC2034 # passed by name local -a denonimations=( "$@" ) + # shellcheck disable=SC2034 # passed by name local -a firstCoinForValue=() if (( amount < 0 )); then @@ -41,7 +43,7 @@ change() { C=(0) S=(0) for ((i = 1; i <= amount; i++)); do - C+=( $max ) + C+=( "$max" ) S+=( "" ) done @@ -64,6 +66,7 @@ change() { make_change() { local -i amount=$1 local -n coins=$2 + # shellcheck disable=SC2178 # $3 references an array, not a string local -n S=$3 local -a change=() local coin idx @@ -76,7 +79,7 @@ make_change() { while (( amount > 0 )); do idx=${S[amount]} coin=${coins[idx]} - change+=( $coin ) + change+=( "$coin" ) (( amount -= coin )) done diff --git a/exercises/practice/clock/.meta/example.sh b/exercises/practice/clock/.meta/example.sh index a61c938f..2f0c1662 100644 --- a/exercises/practice/clock/.meta/example.sh +++ b/exercises/practice/clock/.meta/example.sh @@ -40,7 +40,7 @@ main() { [+-]) (( $# == 4 )) || die "invalid arguments" isnumeric "$4" || die "non-numeric argument" - minutes=$(( minutes $3 $4 )) + [[ $3 == "+" ]] && minutes=$(( minutes + $4 )) || minutes=$(( minutes - $4 )) ;; "") : ;; *) die "invalid arguments" ;; diff --git a/exercises/practice/collatz-conjecture/.meta/example.sh b/exercises/practice/collatz-conjecture/.meta/example.sh index 99237e51..4cd704ab 100644 --- a/exercises/practice/collatz-conjecture/.meta/example.sh +++ b/exercises/practice/collatz-conjecture/.meta/example.sh @@ -9,14 +9,13 @@ fi STEP=0 -while [ "$N" -ne "1" ] -do - if [ "$(($N % 2))" -eq "0" ]; then - N=$(($N / 2)) +while (( N != 1 )); do + if (( N % 2 == 0 )); then + N=$(( N / 2 )) else - N=$((($N * 3) + 1)) + N=$(( N * 3 + 1)) fi - STEP=$(($STEP + 1)) + (( ++STEP )) done -echo $STEP +echo "$STEP" diff --git a/exercises/practice/darts/.meta/example.sh b/exercises/practice/darts/.meta/example.sh index eede4335..93d7e877 100644 --- a/exercises/practice/darts/.meta/example.sh +++ b/exercises/practice/darts/.meta/example.sh @@ -4,6 +4,7 @@ shopt -s extglob main() { (( $# == 2 )) || die "wrong number of arguments" + # shellcheck disable=SC2015 isnumeric "$1" && isnumeric "$2" || die "arguments must be numeric" score_throw "$@" } diff --git a/exercises/practice/diamond/.meta/example.sh b/exercises/practice/diamond/.meta/example.sh index 45e694b2..205dbf5b 100644 --- a/exercises/practice/diamond/.meta/example.sh +++ b/exercises/practice/diamond/.meta/example.sh @@ -5,7 +5,7 @@ error () { } check_args () { # validate the arguments - [[ -z $1 || ${#1} -ne 1 || $# -ne 1 || $1 != ${1//[![:upper:]]/} ]] && error + [[ -z $1 || ${#1} -ne 1 || $# -ne 1 || $1 != "${1//[![:upper:]]/}" ]] && error } pad () { @@ -20,9 +20,9 @@ pad () { line_a () { # print the first or last line padding=$(( line_width / 2 )) - pad $padding + pad "$padding" printf 'A' - pad $padding + pad "$padding" printf "\n" # get a new line } @@ -31,11 +31,11 @@ up_to_max () { int_padding=1 ext_padding=$(( line_width / 2 - 1 )) while [[ $level -le $max_level ]]; do letter="${from_idx_map[$level]}" - pad $ext_padding # external + pad "$ext_padding" # external printf "%s" "$letter" - pad $int_padding # internal + pad "$int_padding" # internal printf "%s" "$letter" - pad $ext_padding # external + pad "$ext_padding" # external printf "\n" ((level++)); ((ext_padding--)) int_padding=$(( int_padding + 2 )) diff --git a/exercises/practice/difference-of-squares/.meta/example.sh b/exercises/practice/difference-of-squares/.meta/example.sh index 074eebab..deed5eba 100644 --- a/exercises/practice/difference-of-squares/.meta/example.sh +++ b/exercises/practice/difference-of-squares/.meta/example.sh @@ -6,7 +6,7 @@ sum_of_squares () { for ((i=1; i<=n; i++)); do squares_summed=$((squares_summed + i ** 2)) done - echo $squares_summed + echo "$squares_summed" } square_of_sum () { diff --git a/exercises/practice/diffie-hellman/.meta/example.sh b/exercises/practice/diffie-hellman/.meta/example.sh index b381ceaa..c98f27c1 100644 --- a/exercises/practice/diffie-hellman/.meta/example.sh +++ b/exercises/practice/diffie-hellman/.meta/example.sh @@ -2,7 +2,7 @@ main() { case $1 in - privateKey) rand 2 $2 ;; + privateKey) rand 2 "$2" ;; publicKey|secret) "$@" ;; *) echo "unknown subcommand" >&2; exit 1 ;; esac diff --git a/exercises/practice/dnd-character/.meta/example.sh b/exercises/practice/dnd-character/.meta/example.sh index 843415ef..f3eed52c 100644 --- a/exercises/practice/dnd-character/.meta/example.sh +++ b/exercises/practice/dnd-character/.meta/example.sh @@ -33,14 +33,15 @@ generate() { for c in "${characteristics[@]}"; do a=$(ability) printf "%s %d\n" "$c" "$a" - [[ $c == constitution ]] && hp=$(( 10 + $(modifier $a) )) + [[ $c == constitution ]] && hp=$(( 10 + $(modifier "$a") )) done echo "hitpoints $hp" } ability() { - set -- $( { d6; d6; d6; d6; } | sort -nr ) - echo $(( $1 + $2 + $3 )) + local -a dice + readarray -t dice < <( { d6; d6; d6; d6; } | sort -nr ) + echo $(( dice[0] + dice[1] + dice[2] )) } # roll a 6-sided die diff --git a/exercises/practice/forth/.meta/example.sh b/exercises/practice/forth/.meta/example.sh index 08ac8a7e..f473ab98 100644 --- a/exercises/practice/forth/.meta/example.sh +++ b/exercises/practice/forth/.meta/example.sh @@ -3,7 +3,7 @@ set -f # disable path exansion shopt -s extglob # enable extended patterns -# I'm going to use the stack and the macros as +# I'm going to use the stack and the macros as # global variables: Not best practice. declare -a stack # indexed array declare -A macros # associative array @@ -37,8 +37,8 @@ main() { word=$1 shift - if is_number $word; then - stack::push $word + if is_number "$word"; then + stack::push "$word" elif [[ -n ${macros[$word]} ]]; then set -- ${macros[$word]} $* else @@ -50,11 +50,11 @@ main() { ;; [-+*/]) # Arithmetic operation - binary_op $word + binary_op "$word" ;; dup|drop|swap|over) # Invoke this word as a command - $word + "$word" ;; *) echo "undefined operation" >&2 @@ -65,12 +65,12 @@ main() { done done - echo ${stack[*]} + echo "${stack[*]}" } record_macro() { local macro_name=$1 - if is_number $macro_name; then + if is_number "$macro_name"; then die "illegal operation: cannot redefine number" fi shift @@ -104,21 +104,21 @@ need() { } binary_op() { - local op=$1 + local op=$1 a b need 2 - local b=$(stack::tail) + b=$(stack::tail) stack::pop - local a=$(stack::tail) + a=$(stack::tail) stack::pop - + [[ $op == "/" ]] && (( b == 0 )) && die "divide by zero" - stack::push $(( a $op b )) + stack::push $(( a "$op" b )) } dup() { need 1 - stack::push $(stack::tail) + stack::push "$(stack::tail)" } drop() { @@ -127,20 +127,22 @@ drop() { } swap() { + local a b need 2 - local b=$(stack::tail) + b=$(stack::tail) stack::pop - local a=$(stack::tail) + a=$(stack::tail) stack::pop - stack::push $b $a + stack::push "$b" "$a" } over() { + local a b need 2 - local b=$(stack::tail) + b=$(stack::tail) stack::pop - local a=$(stack::tail) - stack::push $b $a + a=$(stack::tail) + stack::push "$b" "$a" } main diff --git a/exercises/practice/game-of-life/.meta/example.sh b/exercises/practice/game-of-life/.meta/example.sh index 7deb0e24..5fbdc1e7 100644 --- a/exercises/practice/game-of-life/.meta/example.sh +++ b/exercises/practice/game-of-life/.meta/example.sh @@ -19,6 +19,7 @@ create_matrix() { } tick() { + # shellcheck disable=SC2034 # passed by name local -A next_matrix local -i row col countLiveCell @@ -27,9 +28,11 @@ tick() { countLiveCell=$(check_neighborhood "$col" "$row") if (( countLiveCell == 3 || matrix[${col},${row}] == 1 && countLiveCell == 2 )); then - next_matrix[${col},${row}]=1 + # shellcheck disable=SC2034 # passed by name + next_matrix["${col},${row}"]=1 else - next_matrix[${col},${row}]=0 + # shellcheck disable=SC2034 # passed by name + next_matrix["${col},${row}"]=0 fi done done diff --git a/exercises/practice/grep/.meta/example.sh b/exercises/practice/grep/.meta/example.sh index b1e11621..01f2cb58 100644 --- a/exercises/practice/grep/.meta/example.sh +++ b/exercises/practice/grep/.meta/example.sh @@ -57,9 +57,10 @@ process_file() { printf "%s\n" "$file" break fi + # shellcheck disable=SC2094 output "$num_files" "$file" "$lineno" "$line" fi - done < "$file" + done < "$file" # shellcheck disable=SC2094 } output() {