[SLUG] If there's a competition for the most useless bash script...

Jonathan Worthington jonathan at jwcs.net
Mon Apr 21 00:22:01 BST 2003


Hi,

Well, here's my first effort at a bash script.  It draws a rectangle and
calculates its area.  Maybe one day I'll aspire to something more useful.
;-)

#!/bin/bash
# Rectangle drawing script.  Takes three parameters:-
#  ./rectangle.sh length width [chracater]
# Where character is the character used to print the rectangle.
# If no character is entered, a # is used.
# #####################################################################

# Assign input.
LENGTH=$1
WIDTH=$2
CHAR=$3

# If no char is entered set it to default (#).
if [ -n $CHAR ]; then
     CHAR='#'
fi

# Print a blank line.
echo

# Draw rectangle.
CURWID=0
while [ $CURWID -lt $WIDTH ]; do
     CURLEN=0
     while [ $CURLEN -lt $LENGTH ]; do
          printf "$CHAR"
          CURLEN=$((++CURLEN))
     done
     printf "\n"
     CURWID=$((++CURWID))
done

# Print area.
printf "\nThe area of the rectangle is $[WIDTH*LENGTH].\n\n"

###

Suggestions for optimisations welcomed!

Jonathan






More information about the Scarborough mailing list