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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Cautela<br />

Capitolo 14. Sostituzione <strong>di</strong> comando<br />

L’uso <strong>di</strong> echo per visualizzare una variabile senza quoting, e che è stata impostata<br />

con la sostituzione <strong>di</strong> comando, cancella i caratteri <strong>di</strong> ritorno a capo dall’output<br />

del/dei comando/i riassegnati. Questo può provocare spiacevoli sorprese.<br />

elenco_<strong>di</strong>rectory=‘ls -l‘<br />

echo $elenco_<strong>di</strong>rectory # senza quoting<br />

# Ci si potrebbe aspettare un elenco <strong>di</strong> <strong>di</strong>rectory ben or<strong>di</strong>nato.<br />

# Invece, quello che si ottiene è:<br />

# total 3 -rw-rw-r-- 1 bozo bozo 30 May 13 17:15 1.txt -rw-rw-r-- 1 bozo<br />

# bozo 51 May 15 20:57 t2.sh -rwxr-xr-x 1 bozo bozo 217 Mar 5 21:13 wi.sh<br />

# I ritorni a capo sono scomparsi.<br />

echo "$elenco_<strong>di</strong>rectory" # con il quoting<br />

# -rw-rw-r-- 1 bozo 30 May 13 17:15 1.txt<br />

# -rw-rw-r-- 1 bozo 51 May 15 20:57 t2.sh<br />

# -rwxr-xr-x 1 bozo 217 Mar 5 21:13 wi.sh<br />

La sostituzione <strong>di</strong> comando consente anche <strong>di</strong> impostare una variabile al contenuto <strong>di</strong> un file, sia usando<br />

la re<strong>di</strong>rezione che con il comando cat.<br />

variabile1=‘

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

Saved successfully!

Ooh no, something went wrong!