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.

pm2cpio<br />

Esempio 12-27. Utilizzo <strong>di</strong> cpio per spostare una <strong>di</strong>rectory<br />

#!/bin/bash<br />

# Copiare una <strong>di</strong>rectory usando ’cpio.’<br />

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

# Vantaggi dell’uso <strong>di</strong> ’cpio’:<br />

# Velocità nella copia. Con le pipe è più veloce <strong>di</strong> ’tar’.<br />

# Adatto per la copia <strong>di</strong> file speciali (named pipe, ecc.)<br />

#+ dove ’cp’ potrebbe fallire.<br />

ARG=2<br />

E_ERR_ARG=65<br />

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

then<br />

echo "Utilizzo: ‘basename $0‘ <strong>di</strong>rectory_origine <strong>di</strong>rectory_destinazione"<br />

exit $E_ERR_ARG<br />

fi<br />

origine=$1<br />

destinazione=$2<br />

find "$origine" -depth | cpio -admvp "$destinazione"<br />

# ^^^^^ ^^^^^<br />

# Leggete le pagine <strong>di</strong> manuale <strong>di</strong> ’find’ e ’cpio’ per decifrare queste opzioni.<br />

# Esercizio:<br />

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

# Aggiungete del co<strong>di</strong>ce per verificare l’exit status ($?) della pipe<br />

#+ ’find | cpio’ e che visualizzi degli appropriati messaggi d’errore nel caso<br />

#+ qualcosa non abbia funzionato correttamente.<br />

exit 0<br />

Questo comando crea un archivio cpio da un archivio rpm.<br />

Esempio 12-28. Decomprimere un archivio rpm<br />

#!/bin/bash<br />

# de-rpm.sh: Decomprime un archivio ’rpm’<br />

: ${1?"Utilizzo: ‘basename $0‘ file_archivio"}<br />

# Bisogna specificare come argomento un archivio ’rpm’.<br />

TEMPFILE=$$.cpio # File temporaneo con nome "univoco".<br />

# $$ è l’ID <strong>di</strong> processo dello script.<br />

276

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

Saved successfully!

Ooh no, something went wrong!