Answer the question
In order to leave comments, you need to log in
What does the for loop do in this bash function?
Good day! Help me understand what is happening in the for loop (implementation of the Luhn algorithm for bank cards). Thanks
function validate {
num=$1
len=${#num}
is_odd=1
sum=0
for((t = len - 1; t >= 0; --t)) {
digit=${num:$t:1} #????
if ; then
sum=$(( sum + $digit ))
else
sum=$(( $sum + ( $digit != 9 ? ( ( 2 * $digit ) % 9 ) : 9 ) ))
fi
is_odd=$(( ! $is_odd ))
}
# NOTE: returning exit status of 0 on success
return $(( 0 != ( $sum % 10 ) ))
}
Answer the question
In order to leave comments, you need to log in
What does the for loop do in this bash function?
[[email protected] ~]$ s=abcd
[gues[email protected] ~]$ echo "${s:0:3} ${s:0:2} ${s:2:1}"
abc ab c
[[email protected] ~]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question