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.

# È il compagno <strong>di</strong> “>” e vengono spesso usati insieme.<br />

#<br />

# grep parola-da-cercare File # Scrive la stringa in "File".<br />

exec 3 File # Apre "File" e gli assegna il df 3.<br />

read -n 4 &3 # Scrive il punto decimale dopo i<br />

#+ caratteri letti.<br />

exec 3>&- # Chiude il df 3.<br />

cat File # ==> 1234.67890<br />

# È l’accesso casuale, <strong>di</strong>amine.<br />

|<br />

# Pipe.<br />

# Strumento generico per concatenare processi e coman<strong>di</strong>.<br />

# Simile a “>”, ma con effetti più generali.<br />

# Utile per concatenare coman<strong>di</strong>, script, file e programmi.<br />

cat *.txt | sort | uniq > file-finale<br />

# Or<strong>di</strong>na gli output <strong>di</strong> tutti i file .txt e cancella le righe doppie,<br />

# infine salva il risultato in “file-finale”.<br />

È possibile combinare molteplici istanze <strong>di</strong> re<strong>di</strong>rezione input e output e/o pipe in un’unica linea <strong>di</strong><br />

comando.<br />

comando < file-input > file-output<br />

comando1 | comando2 | comando3 > file-output<br />

Ve<strong>di</strong> Esempio 12-28 e Esempio A-15.<br />

È possibile re<strong>di</strong>rigere più flussi <strong>di</strong> output in un unico file.<br />

ls -yz >> coman<strong>di</strong>.log 2>&1<br />

# Invia il risultato delle opzioni illegali "yz" <strong>di</strong> "ls" nel file "coman<strong>di</strong>.log"<br />

# Poiché lo stderr è stato re<strong>di</strong>retto nel file, in esso si troveranno anche<br />

#+ tutti i messaggi d’errore.<br />

# Notate, però, che la riga seguente *non* dà lo stesso risultato.<br />

ls -yz 2>&1 >> coman<strong>di</strong>.log<br />

# Visualizza un messaggio d’errore e non scrive niente nel file.<br />

396

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

Saved successfully!

Ooh no, something went wrong!