Fireworks From The Command Line - Linux Journal
Fireworks From The Command Line - Linux Journal
To kick off the upcoming 4th of July celebrations we present the following bash script
for your enjoyment.
If you'd rather not try to figure out what it does, watch the video (/video/celebrate-4th-
july-command-line) .
#!/bin/bash
rows=$(tput lines)
cols=$(tput cols)
colors=(red green blue purple cyan yellow brown)
lock_file=
lock_file_base=/tmp/$(basename $0 .sh)
multiple=0
if [[ "$1" ]]; then
nsingle=$1
shift
else
nsingle=10
fi
if [[ "$1" ]]; then
nmultiple=$1
shift
if [[ $nmultiple -gt 8 ]]; then nmultiple=8; fi
else
nmultiple=6
fi
function colorstr()
{
local row=$1
local col=$2
local color=$3
local v
case "$color" in
red) v=31;;
green) v=34;;
blue) v=32;;
purple) v=35;;
cyan) v=36;;
yellow) v=33;;
brown) v=33;;
white) v=37;;
*) v=;;
esac
shift 3
function center_colorstr()
{
local row=$1
local color=$2
shift 2
local s="$*"
local slen=${#s}
colorstr $row $(((cols / 2) - (slen / 2))) $color "$s"
}
function fireworks()
{
local row=$((rows - 1))
local col=$(((RANDOM % (cols / 2)) + (cols / 4)))
local height=$((RANDOM % rows - 2))
local slant
local h
local color1=${colors[$((RANDOM % ${#colors[*]}))]}
local color2=${colors[$((RANDOM % ${#colors[*]}))]}
local color3=${colors[$((RANDOM % ${#colors[*]}))]}
while [[ $color1 == $color2 || $color1 == $color3 || $color2 == $color3 ]]
do
color2=${colors[$((RANDOM % ${#colors[*]}))]}
color3=${colors[$((RANDOM % ${#colors[*]}))]}
done
while [[ $h -gt 0 ]]
do
colorstr $row $col $color1 '.'
let row--
if [[ $((col + slant)) -ge $((cols - 3)) || $((col + slant)) -le 2 ]
let col+=slant
let h--
sleep 0.1
done
if [[ $((col + slant)) -lt $((cols - 3)) && $((col + slant)) -gt 2 ]]; t
h=$((height / 5))
while [[ $h -gt 0 ]]
do
colorstr $row $col $color2 '.'
let row++
if [[ $((col + slant)) -ge $((cols - 3)) || $((col + slant)) -le
let col+=slant
let h--
sleep 0.1
done
fi
clear
http://www.linuxjournal.com/content/fireworks-command-line 2/4
6/10/2017 Fireworks from the Command Line | Linux Journal
pids=
for i in $(seq 1 $nmultiple)
do
let multiple++
lock_file=$lock_file_base.$i
fireworks &
pids="$pids $!"
done
wait $pids
sleep 3
clear
center_colorstr $((rows / 2 - 1)) red "Hope you enjoyed the show!"
center_colorstr $((rows / 2 + 1)) red "Happy 4th of July"
center_colorstr $((rows / 2 + 3)) red "Your Friends at Linux Journal"
echo
sleep 5
clear
Attachment Size
happy4j.sh_.txt 2.89
(http://www.linuxjournal.com/files/linuxjournal.com/ufiles/happy4j.sh_.txt) KB
______________________
Comments
http://www.linuxjournal.com/content/fireworks-command-line 3/4
6/10/2017 Fireworks from the Command Line | Linux Journal
Thanks (/content/fireworks-command-line#comment-339256)
Submitted by JimmyK (not verified) on Sun, 07/05/2009 - 11:32.
Thanks! A tip o' the hat to ye </:-0 . This inspired me to get back to learning bashscripts at
bashscripts.org. I've been neglecting my mind and forgot how to even run a script :-( JimmyK
Copied/pasted script text to new file as well as tired saving/renaming article link for script text
to new file. When run:
???
At some point Ubuntu stopped making bash the default shell and
changed to dash, so if you run the script via "sh happy4j.sh" you
end up running it with dash rather than bash. Run it via "bash (/users/mitch-frazier)
happy4j.sh" or make it executable with "chmod +x happy4j.sh" and then run it directly via
"./happy4j.sh".
I'm not an Ubuntu user so I tend to assume that sh == bash... I probably should stop doing that.
So you may be wondering "what's the value of the shebang (#!) at the top of a script if it doesn't
work with all shells?" Well, the answer is that the shebang is not understood by shells: it's
understood by the O/S itself. That's why making the script executable and running it directly
works.
When the O/S (Linux) opens an executable file (something that has the x bit set) it reads the first
two bytes and checks to see if they are equal to "#!". If they are equal, the O/S then executes the
program specified in the remainder of the first line rather than the original file and passes the
original file name to it as an argument.
Thanks!
Left my bash prompt a red colour after the show - or maybe that's just my eyes having been
dazzled ;)
Ahhh, this brings back memories of getting the latest computer mag in the 80s, typing in the
listing, and hoping for the best. Now, though, I can copy and paste from a web browser into
vim, save, chmod, and sit back and enjoy the show.
http://www.linuxjournal.com/content/fireworks-command-line 4/4