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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

# let "generation += 1" # Increment generation count.<br />

# Set variable to pass as parameter to "display" function.<br />

avar=`echo ${array[@]}` # Convert array back to string variable.<br />

display "$avar"<br />

# Display it.<br />

echo; echo<br />

echo "Generation $generation −− $alive alive"<br />

if [ "$alive" −eq 0 ]<br />

then<br />

echo<br />

echo "Premature exit: no more cells alive!"<br />

exit $NONE_ALIVE # No point in continuing<br />

fi<br />

#+ if no live cells.<br />

}<br />

# =========================================================<br />

# main ()<br />

# Load initial array with contents of startup file.<br />

initial=( `cat "$startfile" | sed −e '/#/d' | tr −d '\n' |\<br />

sed −e 's/\./\. /g' −e 's/_/_ /g'` )<br />

# Delete lines containing '#' comment character.<br />

# Remove linefeeds and insert space between elements.<br />

clear<br />

# Clear screen.<br />

echo #<br />

Title<br />

echo "======================="<br />

echo " $GENERATIONS generations"<br />

echo "<br />

of"<br />

echo "\"Life in the Slow Lane\""<br />

echo "======================="<br />

# −−−−−−−− Display first generation. −−−−−−−−<br />

Gen0=`echo ${initial[@]}`<br />

display "$Gen0"<br />

# Display only.<br />

echo; echo<br />

echo "Generation $generation −− $alive alive"<br />

# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

let "generation += 1"<br />

echo<br />

# Increment generation count.<br />

# −−−−−−− Display second generation. −−−−−−−<br />

Cur=`echo ${initial[@]}`<br />

next_gen "$Cur"<br />

# Update & display.<br />

# −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−<br />

let "generation += 1"<br />

# Increment generation count.<br />

# −−−−−− Main loop for displaying subsequent generations −−−−−−<br />

while [ "$generation" −le "$GENERATIONS" ]<br />

do<br />

Cur="$avar"<br />

Appendix A. Contributed Scripts 384

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

Saved successfully!

Ooh no, something went wrong!