Κυριακή 22 Μαΐου 2011

greek ΑΦΜ

Αντιγράφετε το παρακάτω κείμενο σε έναν επεξεργαστή κειμένου.
Το αποθηκεύετε.
Ορίζετε ότι είναι εκτελέσιμο.
Ετοιμο το προγραμματάκι που ελέγχει αν το ΑΦΜ είναι σωστό. Μπορείτε να το καλείτε από γραμμή εντολών ή από άλλο πρόγραμμα.
Εγώ το ονόμασα isΑΦΜ.
Εσείς μπορείτε να του δώσετε άλλο όνομα.
(Αν πρόκειται να του δώσετε άλλο όνομα αλλάξτε τη γραμμή 27
example usage: user@user-desktop:~$ isΑΦΜ 999888777
με
example usage: user@user-desktop:~$ toonomapoythelete 999888777
ώστε να εμφανίζεται η βοήθεια σωστά.)

#!/bin/bash

# Greek ΑΦΜ check for Linux.
# Takes only one parameter: the ΑΦΜ.
# Exit code 9 if first parameter is not 9 digits long.
# Exit code 8 if first parameter is not only digits.
# Exit code 0 for good ΑΦΜ.
# Exit code 1 or 10 for bad ΑΦΜ.
# Exit code 2 if script goes, for any other reason, to end.
# Copyright (C) 2011. ilias iliadis
# simsonbike-spam@yahoo.gr
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# On Debian GNU/Linux systems, the text of the GPL license,
# version 2, can be found in /usr/share/common-licenses/GPL-2.
LOCALSUCCESS=0
LOCALFUNCTIONFAILURE=3
LOCALUSAGE="example usage: user@user-desktop:~$ isΑΦΜ 999888777"
isdigit () # Tests whether *entire string* is numerical.
{ # In other words, tests for integer variable.
[ $# -eq 1 ] || return $LOCALFUNCTIONFAILURE

case $1 in
*[!0-9]*|"") return $LOCALFUNCTIONFAILURE;;
*) return $LOCALSUCCESS;;
esac
}

#if length of parameter 1 (the only one used for the moment) is not 9
if [[ ${#1} != 9 ]]
# then exit with code 9
then {
echo >&2 'error 9. Not 9 digits in ΑΦΜ (first parameter).'
echo >&2 $LOCALUSAGE
exit 9
}
fi
#if has at only digit characters
if isdigit "$1"
then
# do nothing
:
else
#return error 8
echo >&2 'error 8. Non digits in ΑΦΜ (first parameter).'
echo >&2 $LOCALUSAGE
exit 8
fi

#initialize to zero (just in case ...)
teliko=0
endiameso=0
#depending on the position find the partial checksum
for position in 0 1 2 3 4 5 6 7
do
endiameso=$(( $(( 2 ** $(( 8 - $position )) )) * ${1:$position:1} ))
#and add it to full checksum
teliko=$(( $endiameso + $teliko ))
done
#find the modulo 11 of checksum
modoffirst8=$(( $teliko % 11))
#if modulo 11 equals 10 then check if last digit is 0
if [[ $modoffirst8 = 10 ]]
then {
if [[ ${1:8:1} = 0 ]]
#if yes then ΑΦΜ is OK
then {
echo >&2 "ΑΦΜ OK"
exit $LOCALSUCCESS
}
else {
#else ΑΦΜ is wrong
echo >&2 "error 10. Not a real ΑΦΜ (first parameter)."
exit 10
}
fi
}
#else check if last digit equals modulo
else {
if [[ ${1:8:1} = $modoffirst8 ]]
then {
echo >&2 "ΑΦΜ OK"
exit $LOCALSUCCESS
}
else {
#else ΑΦΜ is wrong
echo >&2 'error 1. Not a real ΑΦΜ (first parameter).'
exit 1
}
fi
}
fi
#just in case
echo >&2 "error 2"
exit 2