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.

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

echo -n "$i" # Ancora, il necessario ’echo’.<br />

let "i += 1" # Incremento.<br />

done‘<br />

echo "variabile2 = $variabile2" # variabile2 = 0123456789<br />

# E’ <strong>di</strong>mostrato che è possibile inserire un ciclo<br />

#+ in una <strong>di</strong>chiarazione <strong>di</strong> variabile.<br />

exit 0<br />

La sostituzione <strong>di</strong> comando permette <strong>di</strong> estendere la serie degli strumenti a <strong>di</strong>sposizione <strong>di</strong><br />

<strong>Bash</strong>. Si tratta semplicemente <strong>di</strong> scrivere un programma, o uno script, che invii il risultato<br />

allo stdout (come fa un ben funzionante strumento UNIX) e <strong>di</strong> assegnare quell’output ad<br />

una variabile.<br />

#include <br />

/* Programma C "Ciao, mondo" */<br />

int main()<br />

{<br />

printf( "Ciao, mondo." );<br />

return (0);<br />

}<br />

bash$ gcc -o ciao ciao.c<br />

#!/bin/bash<br />

# hello.sh<br />

saluti=‘./ciao‘<br />

echo $saluti<br />

bash$ sh hello.sh<br />

Ciao, mondo.<br />

Nota: Nella sostituzione <strong>di</strong> comando, la forma $(COMANDO) ha preso il posto <strong>di</strong> quella con gli<br />

apostrofi inversi.<br />

output=$(sed -n /"$1"/p $file) # Dall’esempio "grp.sh".<br />

388

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

Saved successfully!

Ooh no, something went wrong!