-
-
Save kaimi/7984644 to your computer and use it in GitHub Desktop.
IBAN-Rechner für deutsche Konten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# berechnet zu einer gegebenen deutschen Bankleitzahl-/Kontonummer-Kombination | |
# die zugehörige IBAN | |
# | |
# !ACHTUNG! man beachte | |
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Generierung_der_IBAN | |
# Alle Nummern ohne Gewähr! | |
# | |
blz=$1 | |
knummer=$2 | |
psumme="00" | |
if [[ ! ( "${blz}${knummer}" =~ ^[0-9]+$ ) ]] ; then | |
echo "BLZ/Kontonummer müssen schon Zahlen sein, mein Lieber :)" >&2 | |
exit 1 | |
fi | |
# padding | |
len=${#knummer} | |
for (( i=0; i<10-len; i++ )) | |
do | |
knummer="0${knummer}" | |
done | |
# prüfsumme | |
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Berechnung_der_Pr.C3.BCfsumme | |
psumme=$(expr 98 - "${blz}${knummer}1314${psumme}" % 97) | |
if [ ${#psumme} -ne 2 ] | |
then | |
psumme="0${psumme}" | |
fi | |
# validierung (sicher ist sicher!) | |
# https://de.wikipedia.org/wiki/International_Bank_Account_Number#Validierung_der_Pr.C3.BCfsumme | |
check=$(expr "${blz}${knummer}1314${psumme}" % 97) | |
if [ $check -eq 1 ] | |
then | |
echo "DE${psumme}${blz}${knummer}" | |
else | |
echo "Fehler bei der Berechnung. L2code! :-/" >&2 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my Debain 8.0 system bash does not support 128bit integer calculation. So I separate the modulus calculation into multiple steps.
none of the numbers may have leading 0s at this would lead to the number be considered octal.
Hence the expansion of the account nummber (knummer) should be done after the modulus calculation.