Executable?
So, I am currently working on a Mastermind game in ruby and to run my program I needed to write an executable file. So cool! I have never written an executable before!
Most common files we have are data files, text or images, for example. Executable file contains instructions for the computer to execute and just by specifying the path to the file in the command line the code is run. This is not the same as any file with some code, because to run a code in a regular file we would need enter into command line something like:
ruby <path_to_some_file_with_rb_extension>
On this occasion I need to be able to run my code without using the ruby command, which in itself is just another executable file that was placed when ruby was installed on your system. Instead, I should be able to type the path to this file from my command line and the code will be executed.
Here's my project structure:
mastermind |____bin | |__mastermind | |____lib (contains bunch of ruby files) | |____spec (tests)
The code to be run will live inside the bin/mastermind. It is a convention to put executable files into bin directory. To execute the code in the file I want to be able to just say:
bin/mastermind #if I am currently within the mastermind folder
So how do we make a file executable? What happens now if we type in the above in the command line? Let's run and see what we get. This is what I got (the little emoticon is my custom prompt):
👻 mastermind [master] ⚡ bin/mastermind
zsh: permission denied: bin/mastermind
Permissions
A good place to start would be to check my current permissions for this file:
👻 mastermind [master] ⚡ ls -l bin #asking for detailed list of bin directory content
total 8
-rw-r--r-- 1 Jarkyn staff 312 09 Mar 15:30 mastermind
So if you know exactly what this means and how it works, you may want to skip to the end :). Otherwise, stay tuned!
There are few things we can do with the files, we can read them - r, and we can write - w - to them or we can execute them - x, three generic operations in total, represented by three swithches: rwx. Lets have a detailed look at the output we got when we ran ls -l bin:
____owner permission switches (read - on, write - on, execute - off) | ____group permission switches (read - on, write - off, execute - off) | | ____permission switches for everyone other than owner or group | | | (read - on, write - off, execute - off) ∨ ∨ ∨ - rw- r-- r-- 1 Jarkyn staff 312 09 Mar 15:30 mastermind ∧ ∧ ∧ ∧ ∧ ∧ | | | | | |__file name | | | |__size |__date modified | | | | | |__group file belongs to | | | |__file owner | |_____first dash is not significant to us in this case, it tells that the item is just a file; this is replaced by letter 'd' if item is a directory (other options exist as well, to denote symlink for example)
chmod
We just need to make sure that all users are able to execute my little program, therefore we need to turn last switch for each user type. Here's a command we are going to use: chmod
Here's a simple explanation from Wikipedia; essentially it allows you to change access permissions to files or folders. In my case I want to turn the third switch on for all user types. This is the result I want:
rwx r-x r-x
I will show you the easy version of this command:
chmod a+x bin/mastermind
Let's break down the command:
chmod
: "change permissions"a
: "for all user types"+
: "add" (you could use "-" to remove)x
: "executable permission"bin/mastermind
: path to the file to apply these to
And now if we check our permissions again:
👻 mastermind [master] ⚡ ls -l bin
-rwxr-xr-x 1 Jarkyn staff 312 09 Mar 15:40 mastermind
So far, we essentially told the system to make this file executable. But there are couple of other things I wanted to look at as well, and have not mentioned yet:
- What goes inside of that executable file?
- The other version of the chmod command! The one that uses binary representation of switches converted into decimals
So if you would like to look at those things as well, check out my upcoming posts: Change Mode or Geeking out - Part 2 and TBC or Geeking out - Part 3.