Category Archives: Get Ready

Run the Program

Let’s do some forward thinking here, if we already cretaed our program, how do we run it?

Although we haven’t even learned how to write the program, let’s get us familiar with how we will run it in Linux environment. We know program is a file or a group of files, we also know that obviously not every file is runnable. For example, you can not take a document file and run it, although you can open it by running some other program.

In Linux, to make a file runnable, we give that file a special permission, an executable permission. By running the following command,

chmod a+x file-name

There’s a lot to explain here, but for now let’s just understand that by running the above command, we change the file mode to make it executable, chmod stands for ‘change mode’.

Running the above command to any file will make the file itself executable, but does not turn an arbitrary file to a program, obviously.

It is important though to remember that, if a valid program does not have the executable permission, or in other words not in executable mode, the program is not runnable!

How can we tell if a file is runnable or not?

We can list it in long format, by running ‘ls -l file-name’, and look for ‘x’ in the first column,

-rwxrwxr-x 1 frank frank 0 May 30 22:29 my-program

If we see ‘x’ there, the file is runnable, and likely you can run it. Again, there’s a lot more information encoded there, ‘r’ stands for ‘readable’, and ‘w’ stands for ‘writable’. Why there are more than one ‘x’ and ‘rw’? These have something to do with file permission management, which is something we’ll learn later.

For now, just remember, we must ‘chmod’ a file to add the ‘x’ permission to make it runnable, that’s what the ‘chmod a+x file-name’ for.

If a file is runnable, we can simply type the file name with its path to run it. For example, if the file is at /home/frank/my-programs/hello, the following will run it,

/home/frank/my-programs/hello

Or if we are already at /home/frank, the following will run it,

my-programs/hello

Or if we are already at /home/frank/my-programs, the following will run it,

hello

I lied.

The last one will not work, we have to type file name with its path, if we are already at where the file sits, we still need to type the path, which is current directory. Remember ‘..’ stands for parent directory, ‘.’ stands for current directory.

So, if we are already at /home/frank/my-programs, the following will run it,

./hello

Why? when running something without the path, Linux will try to search that something from a given set of directories, not including current directory. Too complicated? no worries, it is enough for now to remember that we always need to have some path to the file, in order to run it.

Let’s write a simple ‘hello’ program, and play with the executable permission.

Open the file editor, and add the following to the file with name as ‘hello’,

# this is my first program
echo "hello, I am here"
pwd

Save the file to your program directory, for example, the ‘my-programs’ directory we created in last lesson.

Now we have a file named ‘hello’ under /home/frank/my-programs, let’s run it,

./hello

Oops, we have the following error,

bash: ./hello: Permission denied

Let’s give it executable permission,

chmod a+x hello

Run it again,  wow, our first program now works!

hello, I am here
/home/frank/my-programs

Congratulations, we just wrote and run our very first program!

Finally, we are ready to learn programming …

Create New File

Writing program creates new file, we call file that has our program ‘source file’, because it is the ‘source’ of the program.

Before that though, we first need to create a directory to hold all our source files. By having a directory, it will help us find them later easier. So where do we create this directory?

Linux operating system can have multiple users, each user can have his own ‘home’ directory. A ‘home’ directory is where user can create new file. When a different user is using the computer, he’ll get to his own home directory. This way everyone can have his own stuff in a private area, so think ‘home’ directory as your private school locker.

Where is my home directory?

Open a terminal (CTRL + ALT + T), type ‘cd‘ without anything else. Now you are at your home directory.

How do we make sure we indeed are at home, in other words, how do we know where exactly we are? If we are in the middle of a huge tree,  we can easily get lost otherwise.

We’ll learn another useful command here,

  • command: pwd

‘pwd’, Print current Working Directory, tells us where we are in the directory tree.

So type ‘cd’ then ‘pwd’ tells us our home directory is ‘/home/frank’. Remember this is called directory path, the path to our home directory? Let’s explain what this path means,

The first ‘/’ denotes the root directory, this is the root of the tree, and every path starts here. To experiment, the following command will always bring us to the root of directory,

cd  /

The ‘home’ is a subdirectory under root, this is the directory that holds each user’s home directory.

The next ‘/’ indicates there’s more within current directory. Or, this is the separator of multiple sub-directories in any path.

The ‘frank’ is a subdirectory, and this is user Frank’s home directory.

We are home, let’s create a directory called ‘my-programs’, note that I used ‘-‘ instead of space between words. Space is allowed in directory name, but usually using space in directory or file name in Linux/Unix system is a very bad idea. Why? if you try “cd /home/frank/my program/fun games”, you won’t reach the target “fun games” directory, instead it will complain that ‘my’ directory is unable to be found, since anything after the space will not be treated as part of the ‘cd’ command. There’s way to work-around this issue, but we’d rather not to get to this trouble.

In order to create the directory, let’s learn another command,

  • command: mkdir

‘mkdir’, MaKe DIRectory, makes a directory.

Make sure we are at home directory (simply type ‘cd’), then type ‘mkdir my-programs‘, now we have our directory for the great programs we are going to write!

Let’s move on to create our program file …

For our program, we not only need to create it, we need to edit it, probably many times. We need a file editor, what editor shall we use?

Word? Power Point?

Those are all editors, but not the right ones for programming. There are actually many options, emacs can be a good one, however it may take you some time to learn it, although you’ll love it once you learn it. Let’s start with a much easier one, ‘gedit’,

On your terminal, while you are at ‘/home/frank/my-programs’ directory, type ‘gedit hello‘, you’ll see the editor shows up and now you can type whatever. Once you are done editing, click ‘save’, you’ll see ‘hello’ file created in ‘my-programs’ directory.

Great, you just created your first program file under your own directory. Well, this file is not yet a program, but we now are much closer …

Navigating Disk Storage

Before we program, we need to first get us familiar with how and where computer stores programs, so we know what program is available and more importantly, where to find and show off the program that we are going to write.

We know computer has disk somewhere inside, that’s where all data storage happens. We store different sorts of data in the name of ‘file’, the document we write is a file, the picture we draw is a file, the game we download there might have more than one file, and the Operating system itself has a ton of different files. Of course, the program we are going to write is also a file.

A file is a collection of ‘bytes”, where ‘byte’ is the minimal storage unit. So the more ‘bytes’ available in the disk, the more storage space we have. When we say we have a disk of 100GB, we have a disk that has 100 Giga Bytes. One ‘Giga Byte’ has 1,000,000,000 bytes, or rather 1,073,741,824 bytes in computer term. We’ll explain this once we learn the difference between decimal and binary.

We have a big disk that can store lots of files, in order for us to tell which file is which, we need to give each file a unique name. That seems easy, we can simply name each file by a number, say file1, file2, file9999 etc. Right?

The only problem of that approach is we will have lots of magic files that we don’t know what they are for. Hmmm, what is file3789 for again?

Instead, we, or rather Operating System, manage files like a tree. There’s one and only one ‘root’, then there are ‘branches’ above root, there can be more ‘sub-branches’ above any branch, eventually there is ‘leaf’. We call branch as ‘directory’, thus ‘sub-branch’ is ‘sub-directory’, and the ‘leaf’ is our ‘file’.

We can then give each directory/sub-directory and file a unique name, to help us remember which is which. This way, anywhere in the tree, we can climb to find any ‘leaf’, the ‘file’ we want. Any file will be stored at given directory, which can be a sub-directory of other directory. We call this chain of directories that leads to a given file the ‘path’, since this is the ‘path’ we climb in order to find this file.

For example,

/user/frank/games/pvz2/2015/score

So we first climb to ‘user’ branch, then ‘frank’ sub-branch, then ‘games’ sub-branch and so on, eventually we get to the ‘score’ file, this is probably where Frank keeps his pvz2 game score. :-)

This sounds all familiar, yes, this is what the Windows ‘file explorer’ does. When clicking to expand a ‘directory’, we actually climb onto it. When collapsing it, we climb back to its parent ‘directory’. File explorer exists in Linux as well, but we instead will introduce the navigating methods when we are in a command terminal, since that’s also where we learn to run the program we write etc.

It is actually quite simple to navigate directory tree, by knowing only the following two commands and one special case,

1. Command #1: cd

‘cd’ command is used to Change directory. ‘cd <directory-name>’ will navigate from current directory to new directory specified by <directory-name>.

2. Command #2 : ls

‘ls’ command is used to list contents of current directory.

3. Parent directory: ..

‘..’ is a special name for parent directory. For example, ‘cd ..’  will leave current directory and go to its parent directory.

That is all you need to know to navigate directory tree, now open a terminal (by holding CTRL + ALT, and letter ‘t’), and try them out …

‘ls’ command also takes options. Option is additional typing to further change the ‘ls’ command behaviour. For example, ‘ls -a’, will show all contents of current directory, this will show all hidden files as well, if any is available.

It is important to know that most commands come with ‘user manual’ installed. Any time, ‘man <command>’, will print the manual for given <command>. For example, ‘man ls’ will print the detailed user manual for ‘ls’ command. To learn more on any command, simple ‘man <command-name>’. This is most of the time more convenient than asking our Google friend.

Now, try ‘man man’, yes, you guessed it, this will print the user manual for ‘man’ command itself.

With ‘cd’ and ‘ls’, we now are able to navigate the disk, next we’ll try to actually create a file and make sure we can find it as well.

Operating System Setup

We will learn programming on Linux operating system, so we need to first set up one.

Operating system is the first software our Computer gets right after it leaves the production line. Well, the OS might even be installed before the Computer is fully assembled. So if we don’t like or do not want to use the OS that comes with the computer, we need to replace it, right?

So most Computers come with Windows operating system on it, let’s wipe off that and install Linux instead.

But wait, you don’t want to lose all the data there on Windows, more importantly, you don’t want to lose the games you installed there. Programming is fun, but certainly you’ll want to come to those games sometime. :-)

We’ll do it in a non-destructive way. We’ll keep the Windows, and install Linux within Windows!

This is called virtualization, imagine there will be a another *soft* computer within the Windows, we simply install Linux to this *soft* computer.

We call this Computer *soft*,  because it is a computer made of Software.

Ok, let’s create this *soft* computer first by installing a software called VirtualBox, visit the downloads link, download and install VirtualBox for Windows host.

Once that is installed, you can now create as many *soft* computers as you want, and install any Operating System you like on them, including Windows. So you’ll have computer inside of computer, isn’t that cool?

Open VirtualBox, click ‘New’, then follow the on-screen instructions there to create a Computer (or machine), give it 40GB disk, 1GB memory, and 1 CPU.

Now you have a brand new ‘Computer’, let’s install Linux on it.

We’ll be using Ubuntu Linux, visit desktop downloads to get the installation file. Then install it onto the brand new *soft* Computer we just created. Search “Ubuntu virtualbox install”, you’ll find lots of instructions and tutorials. For example, after initial installation, you may also want to install VirtualBox Extension Pack so that the new *soft* Computer display can take up full screen.

Click the newly installed Ubuntu Linux, you now have a Linux operating system running, within Windows!

Next, let’s get familiar with our new Linux friend, as he’ll be the one that helps us programming …