Programming and Scripting

Now, at last, having learned the basics and picked out a text editor, we can get to programming and scripting.

Compiling and Running Programs on Linux

Before compilation, program files are known as “source files”. Source files are generally plain text and can be created either with text editors, or in “integrated developer environments” (IDEs) such as “eclipse”.

Compiled programs are compiled and run in two separate steps. Whenever a change is made, it is made to the “source file” which then needs to be recompiled for the change to be incorporated into the next execution.

Programs are compiled on the command line. During compilation, the source file remains unchanged and the executable file is generated.

In order to run programs in Linux it is also helpful to do two things:

  • Add your current directory or your programming directory into your $PATH. $PATH is an environmental variable set in your .bash_profile file. Remember this if you get a “command not found” error. With java, sometimes the $CLASSPATH also needs to be set. If you need to reset $PATH the instructions for that are here: shell.html
  • Set the permissions properly on your program files so they become executable. Setting permissions is accomplished with chmod. In most circumstances, the correct permissions will be set by default, but they are changeable. If you get a “permission denied” error, you will want to use chmod. More information about chmod

Compiling and Running C++

gcc is the name of the C Compiler. That is the “Gnu C Compiler”. g++ is the command that accesses gcc in C++ mode.

Use a text editor to write the following to a file named “hello.cpp”

hello.cpp
#include <iostream>   // header that provides writing to stdout
using namespace std;    // tells compiler to use standard library
 
int main()
{
  cout << "Hello World!\n";
  return 0;
} 

To compile:

g++ hello.cpp

To see the name (and permissions) of the default compiled file:

ls -l

To run:

./a.out

Compiling and Running Java

For java, the compiler is named javac and the launcher is java. Use a text editor to write the following to a file named “hello.java”

hello.java
class hello {
 
  public static void main(String args[])
    {
       System.out.println("Hello World!");
    }
}

To compile:

javac hello.java

To see the name (and permissions) of the default compiled file:

ls -l

To run:

java hello

Remember to use the class name and not the filename for running java programs.

Interpreted Programs/Scripts on Linux (and notes about shells):

In Linux, “shell scripting” and Perl are often used to automate repetitive tasks.

The more tools you know, the better you can automate tasks in Linux. Perl is beyond the scope of this introduction. Here are the basics of shell scripting:

Shell scripting is similar to running batch files in Microsoft.

In interpreted programs (aka scripts), the first line, also called the “she-bang” line indentifies which program (perl) or which shell to use to interpret the script. The she-bang line consists of the “sharp symbol” (#) plus the “bang symbol” (!) followed by the absolute path to the shell executable file. If you don't know the path, on the command line, type any of the following lines, depending on which shell you want to use:

which sh
which bash
which csh
which ksh

The only thing equivalent to this in the Windows world is if you could (but you can't) tell a batch file to run with DOS commands and filenames, or Windows commands and filenames, or Powershell: right on the top line of the batch file.

Linux uses the “bash shell” by default; that is the “Bourne-Again SHell”. Historically, the very first shell that was created was just named “shell” or “Bourne shell” for the man who invented it. The Bourne shell is invoked by “sh” and is available on all Linux and unix computers. Other shells that are available are csh “C shell” (syntax is more like C programming syntax) , ksh “Korn shell”; there are many more.

Most Linux users prefer bash for its special features like “tab completion” and the ease of accessing “history” from the commandline. More about those later. The bash shell is also very close in syntax to the original Bourne shell (sh) which is an advantage in scripting.

After the she-bang line, all subsequent lines that begin with ”#” aka “octothorpe” aka “pound sign” aka “sharp symbol” aka “hatch mark” aka “tic-tac-toe” aka “number sign” are comment lines. Octothorpe can also be placed in the middle of a line and everything that follows it on the same line (that is, before the line-feed) is a comment. There are no multi-line comment indicators in shell scripting. Empty lines are ignored by the interpreter but are useful for enhancing human readability.

Other than comment lines, each line is treated as a command as if it was written on the command line. Here is an example:

sample.sh
#!/bin/bash     # the she-bang line
 
# this is a comment after an empty line, both these lines are ignored
 
# The next line will print your $PATH to standard output when you run the script
echo "PATH = $PATH"
echo ""     # this prints a blank line
echo "How large is my personal account?"
du -sh $HOME
 
echo ""
echo "Show me a list of the contents in my home directory"
ls $HOME
echo done     # the quotes aren't needed if there are no spaces or special chars
echo ""

Shell scripting also allows loops: for, while and control structures: if-then. More information about how to write shell scripts is available here: http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html

Setting Permissions: chmod To view the permissions in your current directory type:

ls -l

You should see something that looks like:

-rw-r--r-- 1 uwbnix users    3390 Apr  3 21:00 poem1
-rw-r--r-- 1 uwbnix users     581 Apr 15 18:09 poem2
-rwxr--r-- 1 uwbnix users     581 Apr 15 18:09 script.sh
-rw-r--r-- 1 uwbnix users   71189 Aug  4  2004 Sunset.jpg
drwxr-xr-x 3 uwbnix users    4096 Apr  3 20:42 TESTDIR

The first column (leftmost column) are the “permissions” for the files and directories that are represented by the filenames in the last (right side) column. Each “permission” consists of 10 characters. The first one is either ”-” (for a regular file) or “d” (for a directory). It could also be an “l” (for a link). After that first character there are 3 sets of 3, rwx. That is: read, write, execute. If allowed, you will see the “r” or “w” or “x”. If not permitted, a hyphen ”-” will hold the place.

The first set of 3 rwx is the permissions for the owner, in this case: “uwbnix”. The second set of 3 rwx is the permissions for the group: that would be “users”. The third set of permissions is permissions for the world; that is, everyone who can login to the computer who is not either the file owner or in the users group. Or, if you are creating a web page, world permissions are the permissions that your page will pass to the clients' browsers.

There is a very good comprehensive explanation of chmod on wikipedia: http://en.wikipedia.org/wiki/Chmod

To make a script executable, you need to set the executable bit either for just yourself (pick one of two ways - either will work fine):

chmod 744 <scriptname.sh>
chmod u+rwx,g-wx,o-wx <scriptname.sh>

Or for yourself and others in your same group (pick one of two ways - either will work fine):

chmod 754 <scriptname.sh>
chmod u+rwx,g+rx,g-w,o-wx <scriptname.sh>

Linux help is available through linhelp@uwb.edu and

The uwb_linux email list: https://mailman1.u.washington.edu/mailman/listinfo/uwb_linux

Navigation

The best way to find something here is to use the search box in the upper right or the site index link below.

Print/export
Mobile QR Link