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 19. Espressioni Regolari<br />

Il riferimento fondamentale per questo complesso argomento è Mastering Regular Expressions <strong>di</strong> Friedl.<br />

Anche Sed & Awk, <strong>di</strong> Dougherty e Robbins fornisce una luci<strong>di</strong>ssima trattazione delle ER. Ve<strong>di</strong><br />

Bibliografia per ulteriori informazioni su questi libri.<br />

19.2. Globbing<br />

<strong>Bash</strong>, <strong>di</strong> per sé, non è in grado <strong>di</strong> riconoscere le Espressioni Regolari. Negli script, sono i coman<strong>di</strong> e le<br />

utility -- come sed e awk -- che interpretano le ER.<br />

<strong>Bash</strong>, invece, esegue l’espansione del nome del file 2 -- un processo conosciuto come “globbing” -- che,<br />

però, non usa la serie standard <strong>di</strong> caratteri delle ER, ma riconosce ed espande i caratteri jolly. Il globbing<br />

interpreta i caratteri jolly standard * e ?, liste <strong>di</strong> caratteri racchiuse tra parentesi quadre ed alcuni altri<br />

caratteri speciali (come ^, che nega il senso <strong>di</strong> una ricerca). Esistono, tuttavia, alcune importanti<br />

limitazioni nell’impiego dei caratteri jolly. Stringhe che contengono l’* non verificano i nomi dei file che<br />

iniziano con un punto, come, ad esempio, .bashrc. 3 In modo analogo, il ? ha un significato <strong>di</strong>verso da<br />

quello che avrebbe se impiegato in una ER.<br />

bash$ ls -l<br />

total 2<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1<br />

-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh<br />

-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt<br />

bash$ ls -l t?.sh<br />

-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh<br />

bash$ ls -l [ab]*<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1<br />

bash$ ls -l [a-c]*<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1<br />

bash$ ls -l [^ab]*<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1<br />

-rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh<br />

-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt<br />

bash$ ls -l {b*,c*,*est*}<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1<br />

-rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1<br />

-rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt<br />

431

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

Saved successfully!

Ooh no, something went wrong!