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

filter ()<br />

{<br />

# Filter out negative array indices.<br />

echo −n " " # Provides the tilt.<br />

if [[ "$1" −ge 0 && "$1" −lt "$Rows" && "$2" −ge 0 && "$2" −lt "$Columns" ]]<br />

then<br />

let "index = $1 * $Rows + $2"<br />

# Now, print it rotated.<br />

echo −n " ${alpha[index]}" # alpha[$row][$column]<br />

fi<br />

}<br />

rotate () # Rotate the array 45 degrees<br />

{ # ("balance" it on its lower lefthand corner).<br />

local row<br />

local column<br />

for (( row = Rows; row > −Rows; row−− )) # Step through the array backwards.<br />

do<br />

for (( column = 0; column < Columns; column++ ))<br />

do<br />

if [ "$row" −ge 0 ]<br />

then<br />

let "t1 = $column − $row"<br />

let "t2 = $column"<br />

else<br />

let "t1 = $column"<br />

let "t2 = $column + $row"<br />

fi<br />

filter $t1 $t2<br />

done<br />

# Filter out negative array indices.<br />

echo; echo<br />

done<br />

# Array rotation inspired by examples (pp. 143−146) in<br />

# "<strong>Advanced</strong> C Programming on the IBM PC", by Herbert Mayer<br />

# (see bibliography).<br />

}<br />

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

load_alpha # Load the array.<br />

print_alpha # Print it out.<br />

rotate<br />

# Rotate it 45 degrees counterclockwise.<br />

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

# This is a rather contrived, not to mention kludgy simulation.<br />

#<br />

# Exercises:<br />

Chapter 26. Arrays 308

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

Saved successfully!

Ooh no, something went wrong!