27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

fi<br />

return $TRUE<br />

# Valid coordinate.<br />

}<br />

IsAlive ()<br />

{<br />

GetCount "$1" $2<br />

local nhbd=$?<br />

# Test whether cell is alive.<br />

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

# Get alive cell count in neighborhood.<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<br />

# Alive only if previously alive.<br />

return $ALIVE<br />

fi<br />

return $DEAD<br />

# Default.<br />

}<br />

GetCount ()<br />

{<br />

local cell_number=$2<br />

local array<br />

local top<br />

local center<br />

local bottom<br />

local r<br />

local row<br />

local i<br />

local t_top<br />

local t_cen<br />

local t_bot<br />

local count=0<br />

local ROW_NHBD=3<br />

# Count live cells in passed cell's neighborhood.<br />

# Two arguments needed:<br />

# $1) variable holding array<br />

# $2) cell number<br />

array=( `echo "$1"` )<br />

let "top = $cell_number − $COLS − 1" # Set up cell neighborhood.<br />

let "center = $cell_number − 1"<br />

let "bottom = $cell_number + $COLS − 1"<br />

let "r = $cell_number / $ROWS"<br />

for ((i=0; i

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

Saved successfully!

Ooh no, something went wrong!