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.

let "row = $r" # Count center row of neighborhood.<br />

IsValid $t_cen $row<br />

# Valid cell position?<br />

if [ $? −eq "$TRUE" ]<br />

then<br />

if [ ${array[$t_cen]} = "$ALIVE1" ] # Is it alive?<br />

then<br />

# Yes?<br />

let "count += 1"<br />

# Increment count.<br />

fi<br />

fi<br />

let "row = $r − 1"<br />

# Count top row.<br />

IsValid $t_top $row<br />

if [ $? −eq "$TRUE" ]<br />

then<br />

if [ ${array[$t_top]} = "$ALIVE1" ]<br />

then<br />

let "count += 1"<br />

fi<br />

fi<br />

let "row = $r + 1"<br />

# Count bottom row.<br />

IsValid $t_bot $row<br />

if [ $? −eq "$TRUE" ]<br />

then<br />

if [ ${array[$t_bot]} = "$ALIVE1" ]<br />

then<br />

let "count += 1"<br />

fi<br />

fi<br />

done<br />

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

if [ ${array[$cell_number]} = "$ALIVE1" ]<br />

then<br />

let "count −= 1" # Make sure value of tested cell itself<br />

fi<br />

#+ is not counted.<br />

return $count<br />

}<br />

next_gen ()<br />

{<br />

# Update generation array.<br />

local array<br />

local i=0<br />

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

# Convert passed arg to array.<br />

while [ "$i" −lt "$cells" ]<br />

do<br />

IsAlive "$1" $i ${array[$i]}<br />

if [ $? −eq "$ALIVE" ]<br />

then<br />

array[$i]=.<br />

else<br />

array[$i]="_"<br />

fi<br />

let "i += 1"<br />

done<br />

# Is cell alive?<br />

# If alive, then<br />

#+ represent the cell as a period.<br />

# Otherwise underscore<br />

#+ (which will later be converted to space).<br />

Appendix A. Contributed Scripts 383

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

Saved successfully!

Ooh no, something went wrong!