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.

let<br />

<strong>di</strong>r2=/var/spool<br />

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

pushd $<strong>di</strong>r1<br />

# Viene eseguito un ’<strong>di</strong>rs’ automatico (visualizza lo stack delle<br />

#+ <strong>di</strong>rectory allo stdout).<br />

echo "Ora sei nella <strong>di</strong>rectory ‘pwd‘." # Uso degli apici singoli<br />

#+ inversi per ’pwd’.<br />

# Ora si fa qualcosa nella <strong>di</strong>rectory ’<strong>di</strong>r1’.<br />

pushd $<strong>di</strong>r2<br />

echo "Ora sei nella <strong>di</strong>rectory ‘pwd‘."<br />

# Adesso si fa qualcos’altro nella <strong>di</strong>rectory ’<strong>di</strong>r2’.<br />

echo "Nella posizione più alta dell’array DIRSTACK si trova $DIRSTACK."<br />

popd<br />

echo "Sei ritornato alla <strong>di</strong>rectory ‘pwd‘."<br />

# Ora si fa qualche altra cosa nella <strong>di</strong>rectory ’<strong>di</strong>r1’.<br />

popd<br />

echo "Sei tornato alla <strong>di</strong>rectory <strong>di</strong> lavoro originaria ‘pwd‘."<br />

exit 0<br />

# Cosa succede se non eseguite ’popd’ -- prima <strong>di</strong> uscire dallo script?<br />

# In quale <strong>di</strong>rectory vi trovereste alla fine? Perché?<br />

Variabili<br />

Il comando let permette <strong>di</strong> eseguire le operazioni aritmetiche sulle variabili. In molti casi, opera<br />

come una versione meno complessa <strong>di</strong> expr.<br />

Esempio 11-10. Facciamo fare a “let” qualche calcolo aritmetico.<br />

#!/bin/bash<br />

echo<br />

let a=11 # Uguale a ’a=11’<br />

let a=a+5 # Equivale a let "a = a + 5"<br />

# (i doppi apici e gli spazi la rendono più leggibile.)<br />

echo "11 + 5 = $a" # 16<br />

let "a

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

Saved successfully!

Ooh no, something went wrong!