The errors are from missing quotes in the [ ] command: [ "$item" -ne 0 ]
. However, do not use [ ] for arithmetic expressions. Instead, use (( )):
while ((item != 0)); do ... done
Also, your calculation for Armstrong number seems to be wrong. Why are you cubing? You need to check that num has exactly three digits in that case, do you not? http://en.wikipedia.org/wiki/Narcissistic_number
Assuming you really meant the standard definition of "Armstrong number", this should work:
#!/bin/sh -eu
is_armstrong() {
local num digits sum
num="$1"
case "$num" in
# Reject invalid numerals.
(*[^0-9]*|0?*) echo "$num: invalid numeral." >&2; return 1;;
esac
digits=${#num}
sum=0
while ((num > 0)); do
((sum += (num % 10) ** digits))
((num /= 10))
done
((sum == $1))
}
# Prompt the user for a number if none were give on the command line.
if (($# == 0)); then
read -p "Enter a natural number: " num
set -- "$num"
fi
# Process all the numbers.
for num in "$@"; do
num=$((num + 0)) # canonicalize the numeric representation
if is_armstrong "$num"; then
echo "$num is an Amstrong Number"
else
echo "$num is not an Amstrong Number"
fi
done