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 />

L’operatore >> accoda delle righe in un file. Come si può fare, invece, se si desidera anteporre una<br />

riga in un file esistente, cioè, inserirla all’inizio?<br />

file=dati.txt<br />

titolo="***Questa è la riga del titolo del file <strong>di</strong> testo dati***"<br />

echo $titolo | cat - $file >$file.nuovo<br />

# "cat -" concatena lo stdout a $file.<br />

# Il risultato finale è<br />

#+ la creazione <strong>di</strong> un nuovo file con $titolo aggiunto all’*inizio*.<br />

È una variante semplificata dello script <strong>di</strong> Esempio 17-13 già visto in precedenza. Naturalmente, anche<br />

sed è in grado <strong>di</strong> svolgere questo compito.<br />

• Uno script <strong>di</strong> shell può agire come un comando inserito all’interno <strong>di</strong> un altro script <strong>di</strong> shell, in uno<br />

script Tcl o wish, o anche in un Makefile. Può essere invocato come un comando esterno <strong>di</strong> shell in un<br />

programma C, per mezzo della funzione system(), es. system("nome_script");.<br />

• Impostare una variabile al risultato <strong>di</strong> uno script sed o awk incorporato aumenta la leggibilità <strong>di</strong> uno<br />

shell wrapper. Ve<strong>di</strong> Esempio A-1 e Esempio 11-18.<br />

• Si raggruppino in uno o più file le funzioni e le definizioni preferite e più utili. Al bisogno, si può<br />

“includere” uno o più <strong>di</strong> questi “file libreria” negli script per mezzo sia del punto (.) che del comando<br />

source.<br />

# LIBRERIA PER SCRIPT<br />

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

# Nota:<br />

# Non è presente "#!"<br />

# Né "co<strong>di</strong>ce eseguibile".<br />

# Definizioni <strong>di</strong> variabili utili<br />

UID_ROOT=0 # Root ha $UID 0.<br />

E_NONROOT=101 # Errore <strong>di</strong> utente non root.<br />

MAXVALRES=255 # Valore <strong>di</strong> ritorno massimo (positivo) <strong>di</strong> una funzione.<br />

SUCCESSO=0<br />

INSUCCESSO=-1<br />

# Funzioni<br />

Utilizzo () # Messaggio "Utilizzo:".<br />

{<br />

if [ -z "$1" ] # Nessun argomento passato.<br />

then<br />

573

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

Saved successfully!

Ooh no, something went wrong!