camscape - for excellent IT solutions itkb.ro - IT knowledge base

bash :: functie bash pentru aducerea unui numar intreg la o lungime fixa

Cristian
Cristian M.
TitleFunctie bash pentru aducerea unui numar intreg la o lungime fixa
Tagsbash, numar, lungime fixa, 0
Desc.Functie bash pentru aducerea unui numar intreg la o lungime fixa
CodeKBSH0005 v1.0
Date14 septembrie 2012
Functie bash care adauga 0 in fata unui numar intreg pana la o lungime fixa.

Parametrii: numarul intreg, lungimea dorita.

function IntegerLeftPad {
    # Function to convert an integer length X to an integer lenght Y (Y greater
    # than X) by adding 0 in front
    #
    # Param:
    #   - number to convert
    #   - desired length
    #
    # Return:
    #   - converted integer
    #
    # Copyright CAMSCAPE SERVICES GPLv2
    # http://www.camscape.ro
    #
    NUMBER=$1
    LEN=${#NUMBER}
    while [ $LEN -lt $2 ]; do
        NUMBER='0'$NUMBER
        LEN=${#NUMBER}
    done
    echo $NUMBER
}