BASH Scripting – Mystic 8-Ball Oracle
My first article on BASH scripting, where we took a simple number guessing program from the Commodore 64 Users Guide and turned it into a BASH script was so popular https://www.catracing.org/hendrb/bash-script-guess-number/ . We had to do a followup. So to up the ante, I thought I could also reference the blog on BBSing https://www.catracing.org/hendrb/carrier-lost-world-bbs/, and take an IMAGE BBS +.file and attempt to convert it to a BASH script. After all, what is a terminal session anyways, but a big BBS?
For those of you who have not had any exposure to Image BBS or its predecessor C-Net, a Plus file (+.filename, or filename), are basic program modules that are loaded to perform function on the BBS. Each subsystem of the BBS, IE Sub Boards, Upload/Download areas, was a separate program or +. File. Other SysOps could even write their own +. Files, to create more functionality, or write online games.
C-Net, and Image BBS both used a slightly modified BASIC to perform specific tasks, such as outputting text to the screen and modem simultaneously, interpret MCI, etc.
C-Net’s text output command was a$=”Text (K)”:syso
While Image BBS used the & character.
& “Text (K)” (This took up less memory, as it uses fewer characters in the program listing.
Both programs used a Shifted F5 to perform a carriage return, this looked like an inversed K, which is represented here by (K).
Most likely, you’re not here for a BBS history lesson, and want to get down to the programming! So what program did I choose to convert into a BASH script? There was one program on the original Image BBS 1.0 disk that I always had allot of fun with, The Mystical 8 Ball Oracle!
I also thought it would be a great example of some BASH scripting concepts, such as the Do While loop, case statement, and getting to use some ANSI color! One thing about Image BBS, was that it was very colorful! You too can make your BASH scripts stand out by learning ANSI escape sequences.
If you use the echo command you can add COLOR, bold, blinking, and underlined text to your output.
To encode text simply use the –e option of the echo command followed by \e[<ANSI CODE>m
For instance let’s say we want to make our text output bold blue. The ANSI code for bold is 1, and the code for blue text is 44. The line in our BASH script would look like this.
echo –e “\e[1;44mThis is a bold blue line of text\e[0m”
Go ahead and try this at a BASH shell prompt and you should see bold blue text. Here is a good troubleshooting hint. Anything that you can do in a BASH script you can do at the command line. So if something is not working in your script you can break them down line by line at a prompt to find out exactly what is not working.
So let’s break the command above down. We are executing the echo command with the –e (Text encoding option), all ANSI codes entered must be proceeded by the \e and the terminals escape character, in this case the left bracket [. There are two ANSI escape codes in this example separated by the ; 01 specifies bold text, and 44 is the color blue. All ANSI escape sequences must end with the lower case m. The next part of the echo command our actual text, and the \e[0m simply resets the ANSI output to default. So we are not put back at a terminal prompt with a strange color.
Let’s take a look at some common ANSI control codes, that would be useful for your BASH scripts.
So Now that you have an idea of what we will be working with in the program, let’s take a look at our BASH script.
1 #!/bin/bash
2 # /* The Great 8-Ball Oracle */
3 # /* Commodore 64 IMAGE BBS +. File By The Misty Mountain BBS */
4 # /* Included On the IMAGE BBS V1.0 Disk Side 2 */
5 # /* Converted to BASH Script By Brent P Hendricks */
6 # /* Demonstrating Random Numbers, Nested Loops, Case Statement */
7 # /* Check Out Brent’s World, for the acompanied article. */
8 # /* Released to public domain, blog article text (C) 2015 */
9 # /* Brent’s World Blog. www.catracing.org/hendrb */
10
11 PLAY=1
12
13
14 clear
15 echo -e “\n\n \e[1;42mThe Misty Mountain Presents\e[0m”
16 echo -e ” \e[37mWisom Of The Mystic Masters”
17 echo -e ” \e[1;35mThe Mystical 8-Ball Oracle\e[0m”
18 echo -e “\n\n\n\e[37mOriginal Commodore 64 program appeared on side 2 of the IMABE BBS System Disk”
19 echo -e “Converted to BASH Script by Brent Hendricks. To Demonstrate Nested Loop (Do Until),”
20 echo -e “RANDOM Numbers, CASE statement, and ANSI escape codes.”
21 echo -e “\nThis script has been released into the public domain. However the accompanying blog”
22 echo -e “article remains the property of Brent Hendricks, and Brent’s World.”
23 echo -e “\nBe sure to check out Brent’s World @ www.catracing.org/hendrb”
24 echo -e “\n”
25 # echo $PLAY
26 while [ “$PLAY” -eq “1” ]
27 do
28
29 echo -en “\n\nMake Thy Request To The Mystical 8-Ball\nBe Thou Certain, Thy Request May Be Answered Yea or Nay.\n: \e[1;97m”
30 read AN
31 echo -en “\n\e[36mCommencing GURU Meditation.”
32 COUNT=1
33 until [ $COUNT -eq 40 ]
34 do
35 echo -n “.”
36 ((COUNT=$COUNT+1))
37 done
38
39 echo -e “\n\e[0mYou asketh – $AN.”
40 DICE=$((RANDOM % 17 + 1))
41 echo -en “The Answer Is – ”
42 case “$DICE” in
43 1)
44 echo -e “Yes…\n”
45 ;;
46 2)
47 echo -e “No…\n”
48 ;;
49 3)
50 echo -e “Maybe..\n”
51 ;;
52 4)
53 echo -e “Probably Yes..\n”
54 ;;
55 5)
56 echo -e “Probably No..\n”
57 ;;
58 6)
59 echo -e “Undecided..\n”
60 ;;
61 7)
62 echo -e “Of Course..\n”
63 ;;
64 8)
65 echo -e “The Answer Seems Foggy..\n”
66 ;;
67 9)
68 echo -e “Positively\n”
69 ;;
70 10)
71 echo -e “Not A Chance..\n”
72 ;;
73 11)
74 echo -e “The Answer Must Reamin Hidden For Now..\n”
75 ;;
76 12)
77 echo -e “That Is A Silly Question..\n”
78 ;;
79
80 13)
81 echo -e “Yep..\n”
82 ;;
83 14)
84 echo -e “Look For The Answer Within Yourself..\n”
85 ;;
86 15)
87 echo -e “Very Possible..\n”
88 ;;
89
90 16)
91 echo -e “Unlikely..\n”
92 ;;
93 17)
94 echo -e “Nope..\n”
95 ;;
96 esac
97
98 echo -en “Do You have Another Query? y/n : ”
99 read AN
100 if [ $AN = “n” ]
101 then
102 echo -e “\n\n\e[31mThanks For Using. \n\e[33mThe Great 8-Ball Oracle!”
103 echo -e “\e[44Please read the associated blog article @ www.catracing.org/hendrb\e[0m\n”
104 PLAY=0
105 fi
106 done
107 exit 0
One of the hardest parts I found going from Commodore BASIC to BASH scripting is unlearning the use of line numbers. instead of thinking line by line, I have to think in terms of blocks of code. or specific functions. Let’s take a look at what each block in our Magic 8 Ball program actually does.
Line 1, is not really REQUIRED, if you use the BASH shell by default. For portability it is considered best practice to specify for your operating system what shell to use. So Line 1 simply calls the BASH interpreter. the #! is called the she-bang or shebang..
Lines 2-9 are simple comment statements, equivelant to BASICs REM statement. Any line in your code that is preceded by the pound or #, is not acted upon by the interpreter. This is also used in .conf files, to toggle configuration functions on and off. This is called POUNDING OUT. So if you hear someone say, just POUND IT OUT. It means to put a # sign in front of a line in a script of .conf file.
Line 11 sets the variable PLAY to equal 1. It is good practice to setup static variables at the beginning of the script, before they are used in the script. This is used in our Do while Loop, but more on that later.
line 14 clears the screen
Lines 15 – 24 is our scripts title screen. Made up of the echo command, using the -n and or the -e option.
-n tells the echo command that you do not want a NEWLINE (CR/LF) sent after the closing quotes.
-e tells the echo command to interpret escape characters, such as \n (NEW LINE), or \e (ANSI Escape codes)
Line 25 (Which is pounded out), was used for troubleshooting. It is not executed.
Line 26 begins the main portion of our program, which will continue until the PLAY variable is set to 0. Since BASH does not support any type of a GOTO function. We have to set the program as one big loop in order to execute the routine again, if the player so chooses. [ sets up a test routine, which is testing that the PLAY variable is equal -eq to 1/ NOTE: the spaces are there for SYNTAX, not to make it easier to read.
Line 29 establishes our prompt for input, and line 30 reads the keyboard for input (Similar to BASICs INPUT command.), the result is returned in variable AN.
Line 32-37 is a simple do UNTIL loop, similar to a BASIC FOR/NEXT loop. It outputs 40 “.”s to the screen
The loop will be active until the variable COUNT is equal to “40”, until it does it will echo “.” to screen, and the -n option tells echo, not to add a NEWLINE to the end of it’s output. Lilne 35 increments count, by 1.
Line 39 simply echo’s again what the user input, and is stored in variable AN
Line 40 generates a random number between 1 and 17.
Our output is generated between lines 42 – 96 using a CASE statement to mimic BASIC 2.0s DATA statement. The Basic program simply read the data variable x amount of times until it came to the correct response.
IE: 200 DATA “Yes, No, Maybe, … , … , …”, if DICE =3, then the response would have read Maybe.
Line 42 opens the case statement, and each line after that specifies if DICE = x) then perform the tabbed function. You do not have to tab the next lines, but it does make it easier to read.
IE 1)
echo -e “Yes…\n”
Each case function must end with two semi colons. And finally the entire case function is executed with the esac command as seen in line 96.
Line 98 prompts the user if they would like to ask the Mystic 8 ball another question, and the answer is returned in variable AN
If the user answers no, the exit message is displayed, as seen in lines 102-103, and the PLAY variable is set to 0. Which if you remember would exit out DO WHILE loop, and return the user to the main prompt.
If the user answers Y, then PLAY stays set to 1, and the loop begins at the beginning.
If you choose to type the above script in, using the text editor of your choice, remember you must make the script executable.
Save the script as 8ball.sh
At a shell prompt enter chmod 775 8ball.sh
To execute the script, simple enter ./8ball.sh
This should be your output.
Thank you for visiting Brent’s World. We hope you enjoyed another BASH scripting article, we hope that you take the time to experiment with out script. Please feel free to share any comments, and enhancements.
SysOps running Image BBS’s are welcome to copy this blogs text, as long as it is copied in it’s entirety and credit is given to Brent Hendricks, and the URL to Brent’s World is displayed in the text. The original Commodore BASIC program was included on disk 2 of Image BBS v1.0. Image BBS is Copyright New Image Software, and has been released to the public domain. This BASH script is also public domain, however the blog text is Copyright 2015 Brent P Hendricks.
Amazing, always loved magic 8 ball