Bash to Basics: Bash Rocket Science

June 10, 2009 by
Filed under: bash, HowTo, linux, Ubuntu 

Last time in Bash to Basics I showed how to print text to the terminal with the echo command. This got me thinking about the first program I wrote when I was in fourth grade. I was fortunate enough to be in a school district that had computers in the early '80s. If you're around my age, then you may remember the mighty Commodore PET computer.

Some of us were put into a program to learn BASIC programming. The first program we were taught to write made a rocket ship fly up the screen by using Print statements. It looked something like this:

10 REM ROCKET PROGRAM
20 PRINT "     /\"
30 PRINT "    /  \"
40 PRINT "   /    \"
50 PRINT "   |    |"
60 PRINT "   |    |"
70 PRINT "  /|    |\"
80 PRINT " /_|____|_\"
90 END

I thought it would be fun to re-create this old program with bash. So without further ado, here's my first shot at the rocket program.

#!/bin/bash

#rocket - replicates the BASIC rocket program from
#         4th grade

clear
echo '      /\'
echo '     /  \'
echo '    /    \'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '  /|      |\'
echo ' / |      | \'
echo '/  |      |  \'
echo '|__|______|__|'
echo '   / \  / \'
echo '    V    V'
echo
echo
echo

exit 0

Save the above text as a script called rocket1 and make it executable with:

Affiliate Link
chmod +x rocket1

You can now run this script with the command:

./rocket1

Now on the old PET computers, this type of program worked pretty well. You got a nice effect of a rocket ship flying up the screen. Most likely your computer is fast enough that the rocket just appears instantaneously in your terminal window, so we need to figure out how to slow it down a bit.

At first I tried using the sleep command, but the minimum sleep time is one second. That just makes the motion too jittery. I did some searching on the net and found a way to use a perl command to pause the program for less than a second. The command looks like this:

perl -e 'select(undef,undef,undef,.4)'

This is a perl command that will pause a program to allow for user selection. The fourth parameter is the timeout time of 0.4 seconds. The rest of the selection parameters are left undefined. Let's try inserting some of these commands into our program.

#!/bin/bash

#rocket - replicates the BASIC rocket program from 
#         4th grade

clear
echo '      /\'
perl -e 'select(undef,undef,undef,.5)'
echo '     /  \'
perl -e 'select(undef,undef,undef,.4)'
echo '    /    \'
perl -e 'select(undef,undef,undef,.3)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.2)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.1)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.05)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.05)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.05)'
echo '   |      |'
perl -e 'select(undef,undef,undef,.05)'
echo '  /|      |\'
perl -e 'select(undef,undef,undef,.05)'
echo ' / |      | \'
perl -e 'select(undef,undef,undef,.05)'
echo '/  |      |  \'
perl -e 'select(undef,undef,undef,.05)'
echo '|__|______|__|'
perl -e 'select(undef,undef,undef,.05)'
echo '   / \  / \'
perl -e 'select(undef,undef,undef,.05)'
echo '    V    V'
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo

exit 0

Save this as rocket2 and make sure to make it executable with the chmod command.

Running this script works a little better. Notice how the changes in the delay time create a nice acceleration effect.

Not satisfied to leave well enough alone, I wondered if I could create a countdown before the rocket launch. I was able to do it by using some of the advanced options of the echo command. Here's what I came up with.

#!/bin/bash

#rocket - replicates the BASIC rocket program from 
#         4th grade with the addition of a 
#         countdown and acceleration.

clear
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo '      /\'
echo '     /  \'
echo '    /    \'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '   |      |'
echo '  /|      |\'
echo ' / |      | \'
echo '/  |      |  \'
echo '|__|______|__|'
echo '   / \  / \'
echo -e 10\\c
sleep 1
echo -e \\b\\b\\b 9\\c
sleep 1
echo -e \\b\\b\\b 8\\c
sleep 1
echo -e \\b\\b\\b 7\\c
sleep 1
echo -e \\b\\b\\b 6\\c
sleep 1
echo -e \\b\\b\\b 5\\c
sleep 1
echo -e \\b\\b\\b 4\\c
sleep 1
echo -e \\b\\b\\b 3\\c
sleep 1
echo -e \\b\\b\\b 2\\c
sleep 1
echo -e \\b\\b\\b 1\\c
sleep 1
echo -e \\b\\b\\b\\c
echo '    V    V'
perl -e 'select(undef,undef,undef,.4)'
echo
perl -e 'select(undef,undef,undef,.4)'
echo
perl -e 'select(undef,undef,undef,.4)'
echo
perl -e 'select(undef,undef,undef,.3)'
echo
perl -e 'select(undef,undef,undef,.2)'
echo
perl -e 'select(undef,undef,undef,.1)'
echo
perl -e 'select(undef,undef,undef,.07)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo
perl -e 'select(undef,undef,undef,.05)'
echo

exit 0

So save this one as rocket and make it executable with chmod. You can run it with:

./rocket

There you have it, bash rocket science!

Comments

One Response to “Bash to Basics: Bash Rocket Science”

  1. cybormoron says:

    haha, way cool. works good. thanks. gonna share this link with everyone on my buddy list. 10-4 good buddy.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>