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,
Or if we are already at /home/frank, the following will run it,
Or if we are already at /home/frank/my-programs, the following will run it,
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,
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"
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,
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
Congratulations, we just wrote and run our very first program!
Finally, we are ready to learn programming …