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.

val_ritorno 257 # Errore!<br />

echo $? # Restituisce 1 (co<strong>di</strong>ce d’errore generico).<br />

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

val_ritorno -151896 # Funziona con gran<strong>di</strong> numeri negativi?<br />

echo $? # Restituirà -151896?<br />

# No! Viene restituito 168.<br />

# Le versioni <strong>di</strong> <strong>Bash</strong> precedenti alla 2.05b permettevano<br />

#+ valori <strong>di</strong> ritorno <strong>di</strong> gran<strong>di</strong> numeri negativi.<br />

# Quelle più recenti non consentono questa scappatoia.<br />

# Ciò potrebbe rendere malfunzionanti i vecchi script.<br />

# Attenzione!<br />

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

exit 0<br />

Un espe<strong>di</strong>ente per ottenere un intero <strong>di</strong> gran<strong>di</strong> <strong>di</strong>mensioni consiste semplicemente<br />

nell’assegnare il “valore <strong>di</strong> ritorno” ad una variabile globale.<br />

Val_Ritorno= # Variabile globale che contiene un valore <strong>di</strong> ritorno<br />

#+ della funzione maggiore <strong>di</strong> 255.<br />

ver_alt_ritorno ()<br />

{<br />

fvar=$1<br />

Val_Ritorno=$fvar<br />

return # Restituisce 0 (successo).<br />

}<br />

ver_alt_ritorno 1<br />

echo $? # 0<br />

echo "valore <strong>di</strong> ritorno = $Val_Ritorno" # 1<br />

ver_alt_ritorno 256<br />

echo "valore <strong>di</strong> ritorno = $Val_Ritorno" # 256<br />

ver_alt_ritorno 257<br />

echo "valore <strong>di</strong> ritorno = $Val_Ritorno" # 257<br />

ver_alt_ritorno 25701<br />

echo "valore <strong>di</strong> ritorno = $Val_Ritorno" # 25701<br />

Capitolo 23. Funzioni<br />

Un metodo anche più elegante consiste nel visualizzare allo stdout il “valore <strong>di</strong> ritorno” della<br />

funzione con il comando echo e poi “catturarlo” per mezzo della sostituzione <strong>di</strong> comando. Per<br />

una <strong>di</strong>scussione sull’argomento ve<strong>di</strong> la Sezione 33.7.<br />

Esempio 23-10. Confronto <strong>di</strong> due interi <strong>di</strong> gran<strong>di</strong> <strong>di</strong>mensioni<br />

#!/bin/bash<br />

# max2.sh: Maggiore <strong>di</strong> due GRANDI interi.<br />

# È il precedente esempio "max.sh" ,<br />

#+ mo<strong>di</strong>ficato per consentire il confronto <strong>di</strong> gran<strong>di</strong> numeri.<br />

UGUALI=0 # Valore <strong>di</strong> ritorno se i due parametri sono uguali.<br />

E_ERR_PARAM=-99999 # Numero <strong>di</strong> parametri passati alla funzione insufficiente.<br />

455

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

Saved successfully!

Ooh no, something went wrong!