freeshell.de/~shvehlav

~/

Gypsy script - beginner's guide to the occult shell scripting

2021-21-09

If you are any bit like myself, you must have come across the romantic naivety of the occult. Conversing with spirits, offering your bodies to unearthly deities or summoning daemons are a few examples of occult practice usually propelled into the mainstream through books and movies, leaving one wondering what the truth behind it all is or whether there is one at all. I have not personally talked to any ghosts, even though I have tried once during a summer camp - the only result was me being accused of defacing an old tombstone. I have much experience with bodily possession when it comes to various types of flus and viruses, none of which I would title unearthly however. And the only way I ever talked to daemons was through rcctl(8).

And even though the surrealist notion is to purvey the unknown and undebunkable to fish for untapped secrets of the world we reside, we will not be busying ourselves with the aforementioned examples in this article. We won't be questioning the probability of finding banknotes on the sidewalk or weighing the odds of a functioning prayer either. We will instead look into the lenghtily guarded secrets of fortune telling - something I have been warned about by the Czech Association of Magic Card Readers not to do, since it threatens the proprietary methods of maintaining livelihoods of gypsies all over the world. This is something I have chosen to ignore, possibly risking several curses addressed to my name. I have been losing hair by the handfuls lately, so it shouldn't be too hard to get one for ill reasons, if cinematic ways of witchcraft have taught me anything.

While I am not one to rush into replacing our finest fortune tellers by automatisation, fortune(6) has done so for many decades already. We will instead concentrate solely on the art of card reading. More specifically card shuffling. Should any of you find the following tutorial useful in a way which will stop you from visiting your local card reader and replace them by a shell script, at least send them a gift basket and tell them I'm sorry. But as will become obvious, unless you know what each magical card means, you still might want to give the gypsy a call to ask about the cards your UNIX-like machine gave you. On second thought, maybe don't and look up the card meanings online.

This article won't do much to the experienced shell user, but it might be of some use to the completely unexperienced. In the very least, if you are a recent mainstream-OS-refugee, this might not only let the spirits of the occult use your computer to tell your future, but might provide some insight into shell scripting, piping and... that's pretty much it. Because I know the younglings are too busy ricing their desktops with anime themes to read man pages.

Synopsis

This tutorial will let you write your own shell script that will shuffle a deck of cards and read out a chosen number of cards.

What you'll need:

Mommy, what's a script?

A script is an executable file which contains various sets of commands to be executed. Instead of writing several instructions into the shell directly, you can put them in a file and execute them with a single command. If at this point you don't get it, you might get it further on.

First of let us start by making a directory for our experiments someplace safe. I suggest you make a scripts directory in your home.

$ cd
$ mkdir scripts
$ cd scripts
$ mkdir tarot
$ cd tarot

Let us recap what we just did. By calling 'cd' without any argument we moved to our home directory. This is the same as ~/ or /home/YOUR_USERNAME/ by default. Next we made an empty directory called scripts, we moved into it, made a folder called tarot and finally moved into it. You can make sure you are inside the tarot directory with:

$ pwd

You should get something like /home/josef/scripts/tarot . If you get bored during any part of this tutorial, be sure to return to this directory when you pick it back up after 13 days (that's $ cd ~/scripts/tarot).

Alright, we already did some work, but we have not actually began writing our program yet. Next up we will need a file containing a list of cards. You could write such a list yourself, research the internet for a specific magic card set to adopt, or use the one I provide below. Note that these specific cards are taken from an Austria-Hungarian gypsy card set of dubious origin from a grandmother's attic. What do I know, she was not my grandmother.

The only requisite is that each card name occupies a single line of the file. Since there are many ways to do this, I will give you two.

Make sure you're in the tarot folder, open your favorite text editor and copy and paste the list below. Save it as 'list' without the quotes inside the tarot directory.

A popular example of a text editor is vi, but if you are not familiar with it, I suggest using the unfairly treated nano.

$ vi list

OR

$ nano list

Put this in there and save it:

Widdow
Mistress
Road     
House    
Message 
Widdower      
Falsehood
Jealousy
Thief
Merriment
Child
Soldier
Enemy
Lover
Constancy
Thoughts
Love
Marriage
Death
Money
Luck
Gift
Grief
Loyalty
Misfortune
Illness
Priest
Hope
Annoyance
Taxman
Desire
Unexpected joy

Otherwise you can get the following list (while still in the tarot folder!) with:

$ wget freeshell.de/~shvehlav/blog/21-09-21/list

You do need the program 'wget' though.

To make sure the file is there, type:

ls

'list' should be there. If it is not, either you or me are completely lost. It is probably me. But let us work with the reality where the folder 'tarot' now contains the file 'list'.

But because I am paranoid that something went wrong, just make sure the list now contains the set of cards with:

cat list

This should print out what you copied into the file list.

Shuffle

Good job! Now we can finally get into the actual fortune telling.

I can tell you can't wait to try it, so let us do it! We will use the programs 'sort' and 'head'.

$ sort -R list | head -n3

There! Your UNIX-like machine just gave you three randomly shuffled cards from the deck you provided. But how did it do it? First we used the program 'sort' with the flag '-R' to randomly sort the lines in the file 'list'. You can simply try the following a few times:

$ sort -R list

You will get a differently ordered list each time.

The '|' is what is known as a pipe. In layman's terms, think of it as stuffing a pipe with programs that interact with each other in a sequence. We shuffle the list of cards with 'sort -R' and send the shuffled deck to the program 'head' to give us the top three cards with -n3 (you could get a different number of top cards by changing the 3 with a desired number).

You could achieve the same result with multiple lines of commands:

$ sort -R list
$ head -n3 list

But this will spoil the result for you, since you will get the whole shuffled deck after using sort. By using a pipe, you are sending the output of the command 'sort' directly to the next program, which will in turn give you its processed result.

THE SCRIPT

While still in the directory 'tarot' where the list is, open your favorite text-editor and write the following:

#!/bin/sh
sort -R list | head -n3
sort -RC list

The very first line makes sure our system knows what sort of script it is dealing with, in our case a shell script. The second we're already familiar with. The third I added because I have heard somewhere that it is bad luck to not shuffle the deck after reading. The -R flag again shuffles the list, while C (checks that it's shuffled) tells sort to do it quietly without an output.

Next we have to make sure the newly created file 'gypsy' is executable, meaning we can tell it to run. Do so with:

$ chmod +x gypsy 

Perfect, now we can run the program!

$ sh gypsy

And we get 3 randomly selected cards.

"But prahou, I wan't to know what this means!"

Yeah, me too.

Disclaimer

Note that you have to be in the directory ~/scripts/tarot next time you're executing the script. One way to make it work anywhere is to put a full path of the list into the script. Next time we might talk about how we could have achieved the same result with an alias, how I neglected the bin directory and the state of CAMCR stock price.

Further reading

ksh(1)

sort(1)

head(1)

chmod(1)

any book on card reading