Aliens (UNIX Shell Script)
The most complex UNIX Shell Script I wrote as part of my intro to computers class at a fancy out-of-state public college. I think you can figure out what it does from the amazing code below.
# aliens
# written by don burress
# a cshell script designed to entertain the user with a guessing game
# sorry it's so long, i couldn't resist.
# a variable to end it all
set end = ( `echo "KABOOM_____" | tr _ '\007'` )
# welcome the user
echo -n "hello. are you ready to save the world? "
set ans1 = "$<"
if ( "$ans1" =~ n* ) then
echo ""
echo "oh i'm sorry to hear you don't want to save the world."
echo "bye then."
echo "the world is soon destroyed by aliens... :("
echo ""
echo "$end"
sleep 2
exit
endif
if ( "$ans1" !~ y* ) then
echo ""
echo "i assume you mean you want to save the world."
echo "i don't see why you wouldn't..."
echo ""
sleep 2
endif
# instructions
echo ""
echo "you now have a choice."
echo ""
echo "evil aliens not unlike those in Mars Attacks"
echo "have taken over the world and blown up congress."
echo "by guessing the code to their ship through this"
echo "unix-based operating system, you can save the world"
echo "by gaining access to the ship and destroying it."
echo "just type go at the prompt to save the world."
echo "or you can be a sissy and press q ."
echo ""
echo -n "% "
set ans2 = "$<"
echo ""
# quit if they choose q
if ( "$ans2" == q ) then
echo "oh i'm sorry to hear you don't want to save the world."
echo "bye then."
echo "the world is soon destroyed by aliens... :("
echo ""
echo "$end"
sleep 2
exit
endif
# save the world if they didn't choose q
if ( "$ans2" != go ) then
echo "i assume you mean you want to save the world."
echo "i don't see why you wouldn't..."
echo ""
sleep 2
endif
# pick a random number using algorithm from class
@ seed = ( `date +%S` * `date +%S | rev` )
@ number = ( `jot -r 1 1 1000 $seed` )
# make sure number is in limits (not really necessary if "jot" works)
while ( "$number" > 1000 )
while ( "$number" < 1 )
@ number = ( `jot -r 1 1 1000 $seed` )
end
end
# guessing algorithm
set guess = 0
@ guessnum = 0
echo "intelligence reports: the code is a number between 1 and 1000..."
echo "don't even try guessing a letter, the human race will surely perish."
echo -n "guess the code! -- "
while ( "$guess" != "$number" )
set guess = "$<"
echo ""
@ guessnum = ( $guessnum + 1 )
if ( "$guess" > 1000 ) then
echo "try guessing between 1 and 1000, you might get it."
echo -n "try it again! -- "
else if ( "$guess" < 1 ) then
echo "try guessing between 1 and 1000, you might get it."
echo -n "try it again! -- "
else if ( "$guess" < "$number" ) then
echo "hey that's too low!"
echo -n "try again! -- "
else if ( "$guess" > "$number" ) then
echo "hey that's too high!"
echo -n "try again! -- "
# or else they got it
endif
end
echo "you've got the code!""!"
echo ""
# report number of guesses taken
if ( "$guessnum" < 11 ) then
echo "good job soldier you got the code: "$number"."
echo "now blow up that ship with the standard"
echo "thermal detonator set up in this unix-based OS."
else
echo "unfortunately, you took far too long to get "$number"."
echo "an ugly alien soldier steps up behind you and screeches: "
echo "now, young skywalker, you, and the human race, will die!"
echo ""
echo "$end"
sleep 2
exit
endif
echo ""
sleep 3
echo "you place your thermal detonator/unix-based"
echo "operating system on top of the alien guidance"
echo -n "computer"
sleep 1
echo -n ". "
sleep 1
echo -n ". "
sleep 1
echo -n ". "
sleep 1
echo -n ". "
echo "and run like heck."
echo ""
sleep 2
echo "$end"
echo ""
sleep 2
echo "congratulations you have saved the world."
echo "(i wish i could play the final theme from Star Wars for you.)"
echo ""
echo "(but i can't.) bye bye."