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 …

Hello Operating System

Operating System, is the software that makes Computer the robot. Without OS, Computer would be just a piece of paper weight with different metal or plastic parts here and there. OS also provides support to extend the capability of the robot itself, by allowing programmer to further program on it!

OS does many jobs, such as taking our keyboard and mouse input, display information on screen, receive and send data from/to network etc. OS also manages users of Computer, to determine who can use computer when. Ok, I take the ‘when’ part back, Daddy is doing that part, for now.

Out of the many things OS can do, the one thing that interests us programmer is its capability to run the program we write. More interestingly it can run more than one program at the same time.

To run our program, or any program, it is common that we simply pick the program and click on it. To go a little deeper on this, Program is basically a collection of instructions CPU understands. This program needs to be stored somewhere on Computer. Correct, you guessed it, the program is stored in disk. After clicking, in order for CPU to quickly access its next instruction from this program, the program is loaded to memory from disk,  sometime as a whole, sometime only part of it.  After that, CPU runs the instructions one after the other, and all the magic starts.

Clicking is one way to run our program, OS provides us that capability through the graphical interface, you browse to what you want to run, and click. Easy?

That is the graphical way, there’s another way to tell OS to run our program, we call it ‘command line’, because this way we can give OS commands on a line-by-line basis. OS provides us command line access through a program called terminal. So you can open a terminal, type commands, or find our program and run from there.

Wait, it is so easy to click and go, why bother with those extra steps to use command line?

That can be a personal preference, but what if you are told that through command line you can search and manage your files more quickly and easily, and, you can remotely run your program, for example, start your program on your home computer, from your laptop or ipad while you are climbing the Great Wall? And, through command line interface, you can do a lot more than just clicking around.

Command line is a powerful tool for programmer, let’s keep that in mind.

We’ve talked about what OS does and what interface it provides to us in order to run program. But what is this magic piece of OS software called?

There are different flavours of them.

Three among them are well known for Computer world. Windows, OS X, and Linux. There are other Operating Systems for devices, such as Android, iOS etc, but we’ll focus on Computer side.

Linux? never heard of it.

Now you do, and that’s the OS we will be using to learn programming.

Why? read the following text from Wikipedia,

Examples of popular modern operating systems include Android, BlackBerry 10, BSD, Chrome OS, iOS, Linux, OS X, QNX, Microsoft Windows, Windows Phone, and z/OS. The first eight of these examples share roots in UNIX.

Yes, we want to start our learning with something that has a more popular root.

And, because Daddy says so. :-)

Let’s start by setting up a Computer with Linux OS …

Hello Computer

From Wikipedia,

A computer is a general-purpose device that can be programmed to carry out a set of arithmetic or logical operations automatically. Since a sequence of operations can be readily changed, the computer can solve more than one kind of problem.

That seems quite complicated, if I have to know all that to use computer to do my math, I probably will just count on my fingers, or maybe a calculator, if not an abacus.

Let’s describe computer in a more user understandable way. Computer is a robot, it will do exactly what we ask it to do, assuming we speak the language Computer understands of course. Computer ‘listens’ to us through keyboard/mouse, computer ‘tells’ us what it has done through screen, which is often called monitor. Computer ‘friends’ with other electrical devices through its ports: USB, serial etc.

Computer also likes to be social, they ‘talk’ to each other through network, and often exchanges lots of information with each other without us even knowing.

And of course Computer needs power, which is available either from a wall plug or the battery that comes with it.

That is the outside of it, Computer has magic parts inside as well.

First, Computer has storage to store our files such as pictures, movies, and the program that we are going to write. This storage is usually called disk or drive. The important thing for storage is that we can trust them that our files will not go away even if computer lost power.

Then there is a part called memory, so Computer can temporarily hold stuff there without going to actual storage place. You have a full shelf of story books at home,  you’ll still want to put a few of your favourites in your backpack. It is much easier and faster to check out book from your backpack than taking a trip back to home to get it. The key to memory is access speed, but anything Computer puts in memory gets lost when computer lost power. We’ll talk more about memory when we get to actual programming part.

The heart of Computer is called CPU, the Central Processing Uint, this is the big boss, who manages disk and memory, and takes our programming input and commands the other parts of the Computer to do the job.

CPU itself knows about how to work with the other part of the Computer, but does not know how to get the actual job done. Our brain knows how to drive our legs to walk, but we need to learn to get instructions in order to plan a trip to the Great Wall China from Chicago US. CPU gets instructions from its super boss, you, the programmer.

There are many instructions CPU understand, to read or write disk, to read keyboard input and monitor mouse movement, to display text and graphics on monitor, to play sound etc. It is a big task to learn to understand all those instructions all at once, although eventually we’ll learn to program them directly by sending instructions to CPU to drive different part of Computer, we do not start that way. Instead, we start with a piece of software written by other programmers.

This piece of software knows to send the right instructions to CPU to do basic things. For example, if we press letter ‘T’ on keyboard, this software informs the display to also display ‘T’ at the right place. If we move the mouse, this software knows to move the cursor accordingly on screen display. When we are tired, this software will take our request and politely shut down the Computer itself.

Before we start our programming adventure, we have a few words to say about this important piece of software.

This software is called Computer Operating System, or OS …

Hello Programming

Programming is about asking computer to do what we want it to do. Now follow me, “hey Mr. computer, please say hello” …

Well, nothing happens, no matter how polite I am, the computer just sits there doing nothing, or rather, doing its own thing.

To let computer actually do something, we need to be formal, we need to put everything in writing. This writing is called programming.

The above is not entirely true though, computer also takes voice command, for example with ‘Ok Google’ or ‘SiRi’, you can ‘tell’ computer what to do. Although what computer can do in this case is very limited today, this certainly will change. Maybe some day in the future, the programming can be entirely achieved verbally. For now though, we will focus on programming in writing. And hopefully our wonderful programming technique in writing will greatly shorten the time to that future, both ‘Ok Google’ and ‘SiRi’ are programs written by engineers after all.

Back to writing, there is a few questions we need to answer,

  • what do we use to write?
  • what language shall we write in?
  • how do we let computer read our writing?

Answering all that, we’ll have something in writing that the computer can understand, and hopefully computer will actually do what it reads.

This is something in writing is called Program.

To answer all those questions, let’s first take a step back, and see who will actually read our Program and do the things we write down there.

Computer, of course.

That is correct, but there is more. Our program can be read and understood by many more, for example, phone, tablet are the two where we can download program to. There is even more, music player, video game console, TV,  car, many of the electrical things around us can read and run program.

Computer is a little bit special, it understands the Program we write, it is also the tool we use to write our program with. So we will start with Computer …