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.

{}<br />

cp file22.{txt,backup}<br />

# Copia "file22.txt" in "file22.backup"<br />

Capitolo 3. Caratteri speciali<br />

Il comando agisce sull’elenco dei file, separati da virgole, specificati tra le parentesi graffe.<br />

1 L’espansione dei nomi dei file (il globbing) viene applicata a quelli elencati tra le parentesi.<br />

Cautela<br />

Non è consentito alcuno spazio dentro le parentesi, tranne il caso in cui<br />

si utilizzi il "quoting" o se preceduto da un carattere <strong>di</strong> escape.<br />

echo {file1,file2}\ :{\ A," B",’ C’}<br />

file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C<br />

Blocco <strong>di</strong> co<strong>di</strong>ce [parentesi graffe]. Conosciuto anche come “gruppo inline”, questo costrutto crea<br />

una funzione anonima. Tuttavia, a <strong>di</strong>fferenza <strong>di</strong> una funzione, le variabili presenti nel blocco<br />

rimangono visibili alla parte restante dello script.<br />

bash$ { local a;<br />

a=123; }<br />

bash: local: can only be used in a<br />

function<br />

a=123<br />

{ a=321; }<br />

echo "a = $a" # a = 321 (valore <strong>di</strong> a nel blocco <strong>di</strong> co<strong>di</strong>ce)<br />

# Grazie, S.C.<br />

La porzione <strong>di</strong> co<strong>di</strong>ce racchiusa tra le parentesi graffe può avere l’I/O re<strong>di</strong>retto da e verso se stessa.<br />

Esempio 3-1. Blocchi <strong>di</strong> co<strong>di</strong>ce e re<strong>di</strong>rezione I/O<br />

#!/bin/bash<br />

# Legge le righe del file /etc/fstab.<br />

19

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

Saved successfully!

Ooh no, something went wrong!