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.

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

# Questo script è stato ispirato da un articolo, apparso su una notissima<br />

#+ rivista, che magnificava una utility MS Windows <strong>di</strong> 164K avente una<br />

#+ funzionalità simile.<br />

#<br />

# Una buona serie <strong>di</strong> utility per l’elaborazione del testo e un linguaggio<br />

#+ <strong>di</strong> <strong>scripting</strong> efficiente sono una valida alternativa ad eseguibili obesi.<br />

exit 0<br />

Esempio A-2. rn: una semplice utility per rinominare un file<br />

Questo script è una mo<strong>di</strong>fica <strong>di</strong> Esempio 12-19.<br />

#! /bin/bash<br />

#<br />

# Semplicissima utility per "rinominare" i file (basata su "lowercase.sh").<br />

#<br />

# L’utility "ren", <strong>di</strong> Vla<strong>di</strong>mir Lanin (lanin@csd2.nyu.edu),<br />

#+ svolge meglio lo stesso compito.<br />

ARG=2<br />

E_ERR_ARG=65<br />

UNO=1 # Per il corretto uso <strong>di</strong> singolare/plurale<br />

#+ (ve<strong>di</strong> oltre).<br />

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

then<br />

echo "Utilizzo: ‘basename $0‘ vecchio-modello nuovo-modello"<br />

# Come "rn gif jpg", che rinomina tutti i file gif della <strong>di</strong>rectory<br />

#+ <strong>di</strong> lavoro in jpg.<br />

exit $E_ERR_ARG<br />

fi<br />

numero=0 # Per contare quanti file sono stati rinominati.<br />

for nomefile in *$1* # Scorre i file corrispondenti presenti<br />

#+ nella <strong>di</strong>rectory.<br />

do<br />

if [ -f "$nomefile" ] # Se trova corrispondenza...<br />

then<br />

fnome=‘basename $nomefile‘ # Elimina il percorso.<br />

n=‘echo $fnome | sed -e "s/$1/$2/"‘ # Sostituisce il vecchio nome<br />

#+ col nuovo.<br />

mv $fnome $n # Rinomina.<br />

let "numero += 1"<br />

fi<br />

done<br />

if [ "$numero" -eq "$UNO" ] # Per una corretta grammatica.<br />

then<br />

612

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

Saved successfully!

Ooh no, something went wrong!