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.

<strong>di</strong>ff3<br />

s<strong>di</strong>ff<br />

cmp<br />

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

Versione estesa <strong>di</strong> <strong>di</strong>ff che confronta tre file alla volta. Questo comando restituisce, come exit status,<br />

0 in caso <strong>di</strong> successo, ma sfortunatamente non fornisce alcuna informazione sui risultati del<br />

confronto.<br />

bash$ <strong>di</strong>ff3 file-1 file-2 file-3<br />

====<br />

1:1c<br />

Questa è la riga 1 <strong>di</strong> "file-1".<br />

2:1c<br />

Questa è la riga 1 <strong>di</strong> "file-2".<br />

3:1c<br />

Questa è la riga 1 <strong>di</strong> "file-3"<br />

Confronta e/o visualizza due file con lo scopo <strong>di</strong> unirli in un unico file. A causa della sua natura<br />

interattiva, è <strong>di</strong>fficile che questo comando venga impiegato negli script.<br />

Il comando cmp è la versione più semplice <strong>di</strong> <strong>di</strong>ff. Mentre <strong>di</strong>ff elenca le <strong>di</strong>fferenze tra i due file,<br />

cmp mostra semplicemente i punti in cui <strong>di</strong>fferiscono.<br />

Nota: Come <strong>di</strong>ff, cmp restituisce exit status 0 se i file confrontati sono identici, 1 in caso<br />

contrario. Questo ne consente l’impiego per un costrutto <strong>di</strong> verifica in uno script <strong>di</strong> shell.<br />

Esempio 12-32. Utilizzare cmp in uno script per confrontare due file<br />

#!/bin/bash<br />

ARG=2 # Lo script si aspetta due argomenti.<br />

E_ERR_ARG=65<br />

E_NONLEGGIBILE=66<br />

if [ $# -ne "$ARG" ]<br />

then<br />

echo "Utilizzo: ‘basename $0‘ file1 file2"<br />

exit $E_ERR_ARG<br />

fi<br />

if [[ ! -r "$1" || ! -r "$2" ]]<br />

then<br />

echo "Entrambi i file, per essere confrontati, devono esistere"<br />

286

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

Saved successfully!

Ooh no, something went wrong!