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.

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

# Il co<strong>di</strong>ce che si trova oltre questa riga non viene eseguito a causa<br />

#+ del precedente "exit".<br />

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

# Se volete eseguirlo, cancellate o commentate le righe precedenti.<br />

# Lo script visto sopra non funziona con nomi <strong>di</strong> file conteneti spazi<br />

#+ o ritorni a capo.<br />

# Stephane Chazelas, quin<strong>di</strong>, suggerisce l’alternativa seguente:<br />

for file in * # Non è necessario usare basename, perché "*" non<br />

#+ restituisce i nomi <strong>di</strong> file contenenti "/".<br />

do n=‘echo "$file/" | tr ’[:upper:]’ ’[:lower:]’‘<br />

# Notazione POSIX dei set <strong>di</strong> caratteri.<br />

# È stata aggiunta una barra, in modo che gli<br />

# eventuali ritorni a capo non vengano cancellati<br />

# dalla sostituzione <strong>di</strong> comando.<br />

# Sostituzione <strong>di</strong> variabile:<br />

n=${n%/} # Rimuove le barre, aggiunte precedentemente, dal<br />

#+ nome del file.<br />

done<br />

[[ $file == $n ]] || mv "$file" "$n"<br />

exit $?<br />

# Verifica se il nome del file è già in minuscolo.<br />

Esempio 12-20. Du: Conversione <strong>di</strong> file <strong>di</strong> testo DOS al formato UNIX<br />

#!/bin/bash<br />

# Du.sh: converte i file <strong>di</strong> testo DOS in formato UNIX .<br />

E_ERR_ARG=65<br />

if [ -z "$1" ]<br />

then<br />

echo "Utilizzo: ‘basename $0‘ nomefile-da-convertire"<br />

exit $E_ERR_ARG<br />

fi<br />

NUOVONOMEFILE=$1.unx<br />

CR=’\015’ # Ritorno a capo.<br />

# 015 è il co<strong>di</strong>ce ottale ASCII <strong>di</strong> CR<br />

# Le righe dei file <strong>di</strong> testo DOS terminano con un CR-LF.<br />

# Le righe dei file <strong>di</strong> testo UNIX terminano con il solo LF.<br />

tr -d $CR < $1 > $NUOVONOMEFILE<br />

266

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

Saved successfully!

Ooh no, something went wrong!