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.

ash$ echo $PIPESTATUS<br />

141<br />

bash$ ls -al | comando_errato<br />

bash: comando_errato: command not found<br />

bash$ echo $?<br />

127<br />

Capitolo 9. Variabili riviste<br />

Gli elemnti dell’array $PIPESTATUS sono gli exit status dei corrispondenti coman<strong>di</strong> eseguiti nella<br />

pipe. $PIPESTATUS[0] contiene l’exit status del primo comando della pipe, $PIPESTATUS[1]<br />

l’exit status del secondo comando, e così via.<br />

Cautela<br />

La variabile $PIPESTATUS, in una shell <strong>di</strong> login, potrebbe contenere un<br />

errato valore 0 (nelle versioni <strong>Bash</strong> precedenti alla 3.0).<br />

tcsh% bash<br />

bash$ who | grep nobody | sort<br />

bash$ echo ${PIPESTATUS[*]}<br />

0<br />

I coman<strong>di</strong> precedenti, eseguiti in uno script, avrebbero prodotto il<br />

risultato atteso 0 1 0.<br />

Grazie a Wayne Pollock per la puntualizzazione e per aver fornito<br />

l’esempio precedente.<br />

Nota: La variabile $PIPESTATUS, in alcuni contesti, dà risultati inaspettati.<br />

bash$ echo $BASH_VERSION<br />

3.00.14(1)-release<br />

bash$ $ ls | comando_errato | wc<br />

bash: comando_errato: command not found<br />

0 0 0<br />

bash$ echo ${PIPESTATUS[@]}<br />

141 127 0<br />

102

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

Saved successfully!

Ooh no, something went wrong!