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 32. Stile dello <strong>scripting</strong><br />

Ci si abitui a scrivere gli script <strong>di</strong> shell in maniera sistematizzata e strutturata. Anche “al volo” e “scritti<br />

sul retro <strong>di</strong> una busta”, gli script trarranno beneficio se si de<strong>di</strong>cano pochi minuti a pianificare ed<br />

organizzare le idee prima <strong>di</strong> sedersi a co<strong>di</strong>ficarle.<br />

Ecco <strong>di</strong> seguito poche linee guida per lo stile. Non devono essere intese come Regole <strong>di</strong> stile ufficiali per<br />

lo <strong>scripting</strong> <strong>di</strong> shell..<br />

32.1. Regole <strong>di</strong> stile non ufficiali per lo <strong>scripting</strong> <strong>di</strong> shell<br />

• Si commenti il co<strong>di</strong>ce. I commenti rendono più facile agli altri capirlo (e apprezzarlo) e più semplice<br />

la sua manutenzione.<br />

PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"<br />

# Aveva perfettamente senso quando, l’anno scorso, l’avevate scritto, ma<br />

#+ adesso è un mistero totale.<br />

# (Da Antek Sawicki’s "pw.sh" script.)<br />

Si aggiungano intestazioni descrittive agli script e alle funzioni.<br />

#!/bin/bash<br />

#************************************************#<br />

# xyz.sh #<br />

# scritto da Bozo Bozeman #<br />

# 05 luglio 2001 #<br />

# #<br />

# Cancellazione dei file <strong>di</strong> progetto. #<br />

#************************************************#<br />

E_ERRDIR=65 # Directory inesistente.<br />

<strong>di</strong>rprogetti=/home/bozo/projects # Directory da cancellare.<br />

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

# cancella_filep () #<br />

# Cancella tutti i file della <strong>di</strong>rectory specificata. #<br />

# Parametro: $<strong>di</strong>rectory_in<strong>di</strong>cata #<br />

# Restituisce: 0 in caso <strong>di</strong> successo, $E_ERRDIR se qualcosa va storto. #<br />

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

cancella_filep ()<br />

{<br />

if [ ! -d "$1" ] # Verifica l’esistenza della <strong>di</strong>rectory in<strong>di</strong>cata.<br />

then<br />

echo "$1 non è una <strong>di</strong>rectory."<br />

return $E_ERRDIR<br />

fi<br />

541

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

Saved successfully!

Ooh no, something went wrong!