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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

# do<br />

# echo ’denominatore = denominatore * tasso_interesse + 1’<br />

# done<br />

# echo ’denominatore’<br />

# } | bc‘ # Ha inserito il ’ciclo for’ all’interno <strong>di</strong> una<br />

#+ sostituzione <strong>di</strong> comando.<br />

# --------------------------------------------------------------------------<br />

# In aggiunta, Frank Wang suggerisce:<br />

# denominatore=$(echo "scale=9; ($tasso_interesse^$mesi-1)/\<br />

# ($tasso_interesse-1)" | bc)<br />

# Perché . . .<br />

# L’algoritmo che segue il ciclo è,<br />

#+ in realtà, la somma <strong>di</strong> una serie geometrica.<br />

# La formula è e0(1-q^n)/(1-q),<br />

#+ dove e0 è il primo elemento, mentre q=e(n+1)/e(n)<br />

#+ ed n il numero degli elementi.<br />

# --------------------------------------------------------------------------<br />

# let "rata = $numeratore/$denominatore"<br />

rata=$(echo "scale=2; $numeratore/$denominatore" | bc)<br />

# Vengono usate due cifre decimali per i centesimi <strong>di</strong> Euro.<br />

echo<br />

echo "rata mensile = Euro $rata"<br />

echo<br />

exit 0<br />

# Esercizi:<br />

# 1) Filtrate l’input per consentire l’inserimento del capitale con i<br />

# punti <strong>di</strong> separazione.<br />

# 2) Filtrate l’input per consentire l’inserimento del tasso<br />

# d’interesse sia in forma percentuale che decimale.<br />

# 3) Se siete veramente ambiziosi, implementate lo script per visualizzare<br />

# il piano d’ammortamento completo.<br />

Esempio 12-43. Conversione <strong>di</strong> base<br />

#!/bin/bash<br />

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

# Shellscript: base.sh - visualizza un numero in basi <strong>di</strong>fferenti (Bourne Shell)<br />

# Autore : Heiner Steven (heiner.steven@odn.de)<br />

# Data : 07-03-95<br />

# Categoria : Desktop<br />

# $Id : base.sh,v 1.2 2000/02/06 19:55:35 heiner Exp $<br />

# ==> La riga precedente rappresenta l’ID RCS.<br />

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

# Descrizione<br />

317

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

Saved successfully!

Ooh no, something went wrong!