#!/bin/sh -u # parabola loop using Y = X * X adjusted for terminal display # # Integer Arithmetic Only! # Shell scripts only deal in integers. The order in which you do # arithmetic matters! let "x=1/80*160" is zero, because "1/80" is zero # in integer artithmetic. let "x=160*1/80" is the right answer "2". # Always do multiplication before division in integer math. # # Terminal Screen Coordinates are Zero-Based # The "tput cup" program positions the cursor based on coordinates that # range from (0,0) [top left] to (xlimit,ylimit) [bottom right], where # xlimit=cols-1 and ylimit=lines-1. (Using "tput $lines $cols" tries to # position the cursor too far off the bottom right corner of the # screen.) Make sure your display X,Y values lie in the range 0..cols-1 # and 0..lines-1. Trying to draw off the edge of the terminal screen # will result in a character at the edge of the screen. # # NOTE: This script fragment is incomplete - it does not follow all # the script guidelines set out in "script_style.txt". It also # contains many "Instructor-Style" comments that you must not use in # your own scripts. See script_style.txt for details. # # -IAN! idallen@idallen.ca PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 022 # Fetch the current size of the terminal screen. # Create constants for the maximum X and Y terminal output values. # cols=$( tput cols ) lines=$( tput lines ) let xlimit=cols-1 let ylimit=lines-1 # Clear the terminal screen. # tput clear # Subtract one from num because we include the zero point in -num to +num. # Remember that display X must range from "0 to xlimit" not "0 to cols"! # let num=cols/2-1 let x=-num while [ $x -le $num ] ; do let y=x*x # Calculate the display values of X,Y from the program values. # The display values must be non-negative and fit on terminal screen. # To make the parabola appear right-side-up, we have to invert the # display Y values by subtracting from ylimit. # let dispx=x+num let tmpy=y*ylimit/num/num # integer math: do multiplication before division let dispy=ylimit-tmpy # "tput cup" positions the terminal cursor on the terminal screen. # "tput cup" arguments have Y position first, followed by X... # tput cup $dispy $dispx ; echo -n '*' let x=x+1 sleep 0.1 done # Move cursor to second-to-last line on screen. # Clear screen from cursor to end-of-line. # Switch to "bold" terminal font. # Say goodbye. # Switch to "normal" terminal font. # Move cursor to start of last line. # Exit. # let upone=ylimit-1 tput cup $upone 0 tput el # clear screen from cursor to end-of-line tput bold # switch to "bold" terminal font (if any) echo -n "Done parabola xlimit=$xlimit ylimit=$ylimit" tput sgr0 # switch back to normal terminal font tput cup $ylimit 0