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.

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

IsValid () # Test whether cell coor<strong>di</strong>nate valid.<br />

{<br />

if [ -z "$1" -o -z "$2" ] # Mandatory arguments missing?<br />

then<br />

return $FALSE<br />

fi<br />

local row<br />

local lower_limit=0 # Disallow negative coor<strong>di</strong>nate.<br />

local upper_limit<br />

local left<br />

local right<br />

let "upper_limit = $ROWS * $COLS - 1" # Total number of cells.<br />

if [ "$1" -lt "$lower_limit" -o "$1" -gt "$upper_limit" ]<br />

then<br />

return $FALSE # Out of array bounds.<br />

fi<br />

row=$2<br />

let "left = $row * $COLS" # Left limit.<br />

let "right = $left + $COLS - 1" # Right limit.<br />

if [ "$1" -lt "$left" -o "$1" -gt "$right" ]<br />

then<br />

return $FALSE # Beyond row boundary.<br />

fi<br />

return $TRUE # Valid coor<strong>di</strong>nate.<br />

}<br />

IsAlive () # Test whether cell is alive.<br />

# Takes array, cell number, state of cell as arguments.<br />

{<br />

GetCount "$1" $2 # Get alive cell count in neighborhood.<br />

local nhbd=$?<br />

if [ "$nhbd" -eq "$BIRTH" ] # Alive in any case.<br />

then<br />

return $ALIVE<br />

fi<br />

if [ "$3" = "." -a "$nhbd" -eq "$SURVIVE" ]<br />

then # Alive only if previously alive.<br />

return $ALIVE<br />

fi<br />

627

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

Saved successfully!

Ooh no, something went wrong!