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.

Esempio 9-13. Mo<strong>di</strong> alternativi <strong>di</strong> estrarre sottostringhe<br />

#!/bin/bash<br />

# substring-extraction.sh<br />

Capitolo 9. Variabili riviste<br />

Stringa=23skidoo1<br />

# 012345678 <strong>Bash</strong><br />

# 123456789 awk<br />

# Fate attenzione al <strong>di</strong>verso sistema <strong>di</strong> in<strong>di</strong>cizzazione della stringa:<br />

# <strong>Bash</strong> numera il primo carattere della stringa con ’0’.<br />

# Awk numera il primo carattere della stringa con ’1’.<br />

echo ${Stringa:2:4} # posizione 3 (0-1-2), 4 caratteri <strong>di</strong> lunghezza<br />

# skid<br />

# L’equivalente awk <strong>di</strong> ${stringa:pos:lunghezza} è<br />

#+ substr(stringa,pos,lunghezza).<br />

echo | awk ’<br />

{ print substr("’"${Stringa}"’",3,4) # skid<br />

}<br />

’<br />

# Collegando ad awk un semplice comando "echo" gli viene dato un<br />

#+ input posticcio, in questo modo non <strong>di</strong>venta più necessario<br />

#+ fornirgli il nome <strong>di</strong> un file.<br />

exit 0<br />

9.2.2. Ulteriori approfon<strong>di</strong>menti<br />

Per altro materiale sulla manipolazione delle stringhe negli script, si faccia riferimento alla Sezione 9.3 e<br />

all’ importante sezione relativa all’elenco dei coman<strong>di</strong> expr. Per gli script d’esempio, si veda:<br />

1. Esempio 12-9<br />

2. Esempio 9-16<br />

3. Esempio 9-17<br />

4. Esempio 9-18<br />

5. Esempio 9-20<br />

9.3. Sostituzione <strong>di</strong> parametro<br />

126

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

Saved successfully!

Ooh no, something went wrong!