11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Messaggio2="un abitante della Terra."<br />

echo<br />

Capitolo 11. Coman<strong>di</strong> interni e builtin<br />

printf "Pi con 2 cifre decimali = %1.2f" $PI<br />

echo<br />

printf "Pi con 9 cifre decimali = %1.9f" $PI # Esegue anche il corretto<br />

#+ arrotondamento.<br />

printf "\n" # Esegue un ritorno a capo,<br />

# equivale a ’echo’.<br />

printf "Costante = \t%d\n" $CostanteDecimale # Inserisce un carattere<br />

#+ <strong>di</strong> tabulazione (\t)<br />

printf "%s %s \n" $Messaggio1 $Messaggio2<br />

echo<br />

# ==================================================#<br />

# Simulazione della funzione sprintf del C.<br />

# Impostare una variabile con una stringa <strong>di</strong> formato.<br />

echo<br />

Pi12=$(printf "%1.12f" $PI)<br />

echo "Pi con 12 cifre decimali = $Pi12"<br />

Msg=‘printf "%s %s \n" $Messaggio1 $Messaggio2‘<br />

echo $Msg; echo $Msg<br />

# Ora possiamo <strong>di</strong>sporre della funzione ’sprintf’ come modulo<br />

#+ caricabile per <strong>Bash</strong>. Questo, però, non è portabile.<br />

exit 0<br />

# N.d.T. Nella versione originale veniva usato il punto come separatore<br />

#+ decimale. Con le impostazioni locali italiane il punto avrebbe<br />

#+ impe<strong>di</strong>to il corretto funzionamento <strong>di</strong> printf.<br />

Un’utile applicazione <strong>di</strong> printf è quella <strong>di</strong> impaginare i messaggi d’errore<br />

E_ERR_DIR=65<br />

var=<strong>di</strong>rectory_inesistente<br />

errore()<br />

{<br />

printf "$@" >&2<br />

# Organizza i parametri posizionali passati e li invia allo stderr.<br />

echo<br />

192

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!