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.

Capitolo 33. Miscellanea<br />

33.1. Shell e script interattivi e non<br />

Nobody really knows what the Bourne<br />

shell’s grammar is. Even examination of<br />

the source code is little help.<br />

Tom Duff<br />

Una shell interattiva legge i coman<strong>di</strong> dall’input dell’utente, immessi da una tty. Una tale shell, in modo<br />

predefinito, legge i file <strong>di</strong> avvio in fase <strong>di</strong> attivazione, visualizza un prompt e abilita il controllo dei job,<br />

tra le altre cose. L’utente può interagire con la shell.<br />

Una shell che esegue uno script è sempre una shell non interattiva. Tuttavia, lo script può ancora<br />

accedere alla sua tty. È anche possibile simulare, nello script, una shell interattiva.<br />

#!/bin/bash<br />

MIO_PROMPT=’$ ’<br />

while :<br />

do<br />

echo -n "$MIO_PROMPT"<br />

read riga<br />

eval "$riga"<br />

done<br />

exit 0<br />

# Questo script d’esempio e gran parte della spiegazione precedente<br />

#+ sono stati forniti da Stéphane Chazelas (grazie ancora).<br />

Si considera come interattivo quello script che richiede l’input dall’utente, <strong>di</strong> solito per mezzo <strong>di</strong><br />

enunciati read (ve<strong>di</strong> Esempio 11-3). La “realtà” , a <strong>di</strong>re il vero, è un po’ meno semplice <strong>di</strong> così. Per il<br />

momento si assume che uno script interattivo sia quello connesso ad una tty, uno script che un utente ha<br />

invocato da console o da xterm.<br />

Gli script init e <strong>di</strong> avvio sono, per forza <strong>di</strong> cose, non interattivi, perché devono essere eseguiti senza<br />

l’intervento umano. Allo stesso modo, non sono interattivi gli script che svolgono attività<br />

d’amministrazione e <strong>di</strong> manutenzione del sistema. Compiti invariabili e ripetitivi richiedono <strong>di</strong> essere<br />

svolti automaticamente per mezzo <strong>di</strong> script non interattivi.<br />

Gli script non interattivi possono essere eseguiti in background, a <strong>di</strong>fferenza <strong>di</strong> quelli interattivi che si<br />

bloccano in attesa <strong>di</strong> un input che potrebbe non arrivare mai. Questa <strong>di</strong>fficoltà può essere gestita con uno<br />

script expect o con l’inserimento <strong>di</strong> un here document che sono in grado <strong>di</strong> fornire l’input allo script<br />

545

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

Saved successfully!

Ooh no, something went wrong!