56 lines
1.0 KiB
Bash
56 lines
1.0 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Strip all spaces from input
|
|
input=$(echo $1 | tr -d ' ')
|
|
|
|
# Check input length
|
|
if [ ${#input} -le 1 ]
|
|
then
|
|
echo "false"
|
|
exit 0
|
|
fi
|
|
|
|
# Check for non-digit characters
|
|
if [[ $input =~ [^[:digit:]] ]]
|
|
then
|
|
echo "false"
|
|
exit 0
|
|
fi
|
|
|
|
# Double every second digit from the right
|
|
doubled_input=""
|
|
for (( i=${#input}-1; i>=0; i-- ))
|
|
do
|
|
# Checks if the current digit is a second digit
|
|
if [ $(((${#input}-i) % 2)) -eq 0 ]
|
|
then
|
|
doubled_digit=$(( ${input:$i:1} * 2 ))
|
|
if [ $doubled_digit -gt 9 ]
|
|
then
|
|
doubled_digit=$(( doubled_digit - 9 ))
|
|
fi
|
|
# Adds the new digit to the left of "doubled_input"
|
|
doubled_input="${doubled_digit}${doubled_input}"
|
|
# If is not a second digit, copy it to the new string.
|
|
else
|
|
doubled_input="${input:$i:1}${doubled_input}"
|
|
fi
|
|
done
|
|
|
|
# Calculate the sum of all digits
|
|
sum=0
|
|
for (( i=0; i<${#doubled_input}; i++ ))
|
|
do
|
|
sum=$(( sum + ${doubled_input:$i:1} ))
|
|
done
|
|
|
|
# Check if sum is divisible by 10
|
|
if [ $(( sum % 10 )) -eq 0 ]
|
|
then
|
|
echo "true"
|
|
else
|
|
echo "false"
|
|
fi
|