Skip to main content
added 638 characters in body
Source Link
user1133275
  • 5.7k
  • 1
  • 22
  • 40

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

for stack traces if you add the following to the top of all your scripts it will inform you of errors;

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR 

and useit will stop the script on the error line, make output like

test.sh: line 7: no: command not found ERROR: test.sh:7 no + 5 

instead of (potentially silently) ignoring errors. Use -x for debugging;

bash -x armstrong.sh 222 ++ echo -n 222 ++ wc -c + P=3 + SUM=0 ++ fold -w 1 ++ echo 222 + for X in $(echo "$1" | fold -w 1) ++ echo '0+(2^3)' ++ bc + SUM=8 + for X in $(echo "$1" | fold -w 1) ++ echo '8+(2^3)' ++ bc + SUM=16 + for X in $(echo "$1" | fold -w 1) ++ echo '16+(2^3)' ++ bc + SUM=24 + echo 24 24 

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

for stack traces if you add the following to the top of all your scripts it will inform you of errors;

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR 

and use -x for debugging;

bash -x armstrong.sh 222 ++ echo -n 222 ++ wc -c + P=3 + SUM=0 ++ fold -w 1 ++ echo 222 + for X in $(echo "$1" | fold -w 1) ++ echo '0+(2^3)' ++ bc + SUM=8 + for X in $(echo "$1" | fold -w 1) ++ echo '8+(2^3)' ++ bc + SUM=16 + for X in $(echo "$1" | fold -w 1) ++ echo '16+(2^3)' ++ bc + SUM=24 + echo 24 24 

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

for stack traces if you add the following to the top of all your scripts it will inform you of errors;

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR 

it will stop the script on the error line, make output like

test.sh: line 7: no: command not found ERROR: test.sh:7 no + 5 

instead of (potentially silently) ignoring errors. Use -x for debugging;

bash -x armstrong.sh 222 ++ echo -n 222 ++ wc -c + P=3 + SUM=0 ++ fold -w 1 ++ echo 222 + for X in $(echo "$1" | fold -w 1) ++ echo '0+(2^3)' ++ bc + SUM=8 + for X in $(echo "$1" | fold -w 1) ++ echo '8+(2^3)' ++ bc + SUM=16 + for X in $(echo "$1" | fold -w 1) ++ echo '16+(2^3)' ++ bc + SUM=24 + echo 24 24 
added 638 characters in body
Source Link
user1133275
  • 5.7k
  • 1
  • 22
  • 40

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

for stack traces if you add the following to the top of all your scripts it will inform you of errors;

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR 

and use -x for debugging;

bash -x armstrong.sh 222 ++ echo -n 222 ++ wc -c + P=3 + SUM=0 ++ fold -w 1 ++ echo 222 + for X in $(echo "$1" | fold -w 1) ++ echo '0+(2^3)' ++ bc + SUM=8 + for X in $(echo "$1" | fold -w 1) ++ echo '8+(2^3)' ++ bc + SUM=16 + for X in $(echo "$1" | fold -w 1) ++ echo '16+(2^3)' ++ bc + SUM=24 + echo 24 24 

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.

for stack traces if you add the following to the top of all your scripts it will inform you of errors;

set -e trap 'echo "ERROR: $BASH_SOURCE:$LINENO $BASH_COMMAND" >&2' ERR 

and use -x for debugging;

bash -x armstrong.sh 222 ++ echo -n 222 ++ wc -c + P=3 + SUM=0 ++ fold -w 1 ++ echo 222 + for X in $(echo "$1" | fold -w 1) ++ echo '0+(2^3)' ++ bc + SUM=8 + for X in $(echo "$1" | fold -w 1) ++ echo '8+(2^3)' ++ bc + SUM=16 + for X in $(echo "$1" | fold -w 1) ++ echo '16+(2^3)' ++ bc + SUM=24 + echo 24 24 
Source Link
user1133275
  • 5.7k
  • 1
  • 22
  • 40

Use https://www.shellcheck.net (I use it as syntactic vim plugin making a crude IDE)

I'd go with this;

#!/bin/bash P="$(echo -n "$1" | wc -c)" SUM=0; for X in $(echo "$1" | fold -w 1) ; do SUM=$(echo "$SUM+($X^$P)" | bc ); done echo "$SUM" 

It's not "pure" bash but I find the power of bash is the wide selection of tools, and prioritizing legibility.