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.

cd<br />

Filesystem<br />

exit 0 # Fine del co<strong>di</strong>ce.<br />

# Segue l’output (parziale) dello script.<br />

# ’echo’ fornisce le parentesi graffe aggiuntive.<br />

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

./readpipe.sh<br />

{#!/bin/sh}<br />

{ultimo="(null)"}<br />

{cat $0 |}<br />

{while read riga}<br />

{do}<br />

{echo "{$riga}"}<br />

{ultimo=$riga}<br />

{done}<br />

{printf "nFatto, ultimo:$ultimon"}<br />

Fatto, ultimo:(null)<br />

Capitolo 11. Coman<strong>di</strong> interni e builtin<br />

La variabile (ultimo) è stata impostata all’interno <strong>di</strong> una subshell,<br />

al <strong>di</strong> fuori <strong>di</strong> essa, quin<strong>di</strong>, rimane non impostata.<br />

Lo script gen<strong>di</strong>ff, che <strong>di</strong> solito si trova in /usr/bin in molte <strong>di</strong>stribuzioni Linux, usa una pipe per<br />

collegare l’output <strong>di</strong> find ad un costrutto while read.<br />

find $1 \( -name "*$2" -o -name ".*$2" \) -print |<br />

while read f; do<br />

. . .<br />

Il familiare comando <strong>di</strong> cambio <strong>di</strong> <strong>di</strong>rectory cd viene usato negli script in cui, per eseguire un certo<br />

comando, è necessario trovarsi in una <strong>di</strong>rectory specifica.<br />

(cd /source/<strong>di</strong>rectory && tar cf - . ) | (cd /dest/<strong>di</strong>rectory && tar xpvf -)<br />

[dal già citato esempio <strong>di</strong> Alan Cox]<br />

L’opzione -P (physical) <strong>di</strong> cd permette <strong>di</strong> ignorare i link simbolici.<br />

cd - cambia a $OLDPWD, la <strong>di</strong>rectory <strong>di</strong> lavoro precedente.<br />

199

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

Saved successfully!

Ooh no, something went wrong!