Ero lì bel bello che mi creavo uno scriptino sciocco per segnalare la vita di H per prendere un po’ la mano con le shell Bash.

Tutto molto bello, se non fosse che il fantastico comando

date +%j

restituisce (come da manuale) il giorno dell’anno in tre cifre xxx

%j day of year (001..366)

Ora, nel momento in cui vi mettete a fare dei conti (somme e differenze, niente di complesso: tipo 053-35) non vi mettete a considerare gli 0 davanti ad un intero.
Ma Bash, che è estremamente furbo, decide che

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arithmetic base, and n is a number in that base. If base# is omitted, then base 10 is used.

Quindi il furbone mi prendeva 053 come un’ottale dall’interessante valore decimale di 43! Ovviamente qualunque conteggio risultava sbagliato.

E così se dovete fare dei conti in shell bash, meglio definire la base:

sim@idrogeno:~$ date +%j
053
sim@idrogeno:~$ echo $((10#`date +%j`))
53




Write a comment


You need tologin.

    
SIMOTRONE WEB PAGE is based on WordPress platform, RSS tech , RSS comments design by Gx3.