A Computer programming is defined as the process of designing and building an executable program for accomplishing a specific computing task. There are different types of programs to do this. We will be seeing how to execute them in a Linux machine.

    An interpreter is the one that helps to execute a program. Every program has an individual interpreter.

A program may be classified as,

1.Compiler Programs.
2.Direct Programs.

Compiler Programs - Programs that uses compilers before executing
  • C
  • C++
  • Java
Direct Programs - Programs that can be executed directly
  • Python
  • Perl
  • Ruby
  • Bash
    As i said earlier in this article we will see how to execute different programs that will print "Testing Program for Linux!!!".

Compiler Programs:

C:


    A C program has an extension .c, In C programs we need to compile the program before executing. For this we need gcc(GNU Compiler Collections) to be installed in our Linux machine.

    Install gcc with the below commands,

For Debian machines:

$ sudo apt-get install build-essential

For RPM machines:

$ sudo yum install gcc

Check the version:

$ gcc –version

Create a file with your favorite text editor as myfile.c.

jhony@ljlinux:/home/programs$ vim myfile.c

Paste the below code inside the file.

Code:
#include <stdio.h>
int main()
{

   printf("Testing Program for Linux!!!\n");
   return 0;

}

jhony@ljlinux:/home/programs$ ls
myfile.c

Compiling:

    Compile the program using gcc command.

jhony@ljlinux:/home/programs$ gcc myfile.c

jhony@ljlinux:/home/programs$ ls
a.out  myfile.c

    Now you will get an executable file as a.out

Executing:

    You can either execute the file using ./ or calling the whole path of the file.

jhony@ljlinux:/home/programs$ ./a.out
Testing Program for Linux!!!

C++:


    A C++ program has the file extension .cc, Same as C programs we need to compile the program before executing it.

Create a file with your favorite text editor as myfile.c.

jhony@ljlinux:/home/programs$ vim myfile.cc

Paste the code inside the file.

Code:
#include <iostream>

int main()
{
  std::cout << "Testing Program for Linux!!!" << std::endl;
  return 0;
}

jhony@ljlinux:/home/programs$ ls
myfile.cc

Compiling:

Compile the program using g++ command.

jhony@ljlinux:/home/programs$ g++ myfile.cc

You can also have a specify output file for your program by passing -o flag while compiling,

jhony@ljlinux:/home/programs$ g++ -o  myfile myfile.cc

jhony@ljlinux:/home/programs$ ls
a.out  myfile  myfile.cc

Executing:

    You can either execute the file using ./ or calling the whole file of the path.

jhony@ljlinux:/home/programs$ ./a.out
Testing Program for Linux!!!

jhony@ljlinux:/home/programs$ ./myfile
Testing Program for Linux!!!

Java:


    Java files (.java) cannot be executed directly. Hence it also requires a compiler.

    In Java the compiler's job is to translate Java files into "class files" which are binary coded that the virtual machine can execute. These virtual machines are termed as Java Virtual Machine(JVM) .

Create a file name as myfile.java

jhony@ljlinux:/home/programs$ vim myfile.java

Paste the below code inside the file.

Code:
class TestingProgram
{
    public static void main(String[] args)
    {
        // prints Testing Program for Linux!!! on console
        System.out.println("Testing Program for Linux!!!");
    }
}

Compiling the Program:

    The java compiler javac creates a .class file with a prefix of your main class. The .class file contains only binary code of your program. As said earlier, the Java byte-code is the intermediate representation of Testing Program.java program that contains instructions the Java interpreter will execute.

jhony@ljlinux:/home/programs$ javac myfile.java

jhony@ljlinux:/home/programs$ ls
myfile.java  TestingProgram.class

Note: The Java compiler (javac) can compile multiple .java files together.

jhony@ljlinux:/home/programs$ javac myfile.java myfile1.java myfile2.java

Or you can use a wild card operator * to compile all files

jhony@ljlinux:/home/programs$ javac *.java

Executing the Program:

    After compilation of Your_Program.java to Your_Program.class, we use the Java interpreter to execute it which looks for the main class. Hence you will be using your class name for execution not your file name.

jhony@ljlinux:/home/programs$ java TestingProgram
Testing Program for Linux!!!

If java is not installed in your system please click here to Install Java.

Direct Programs:

    The following programs can be executed directly. Which means it uses an inbuilt compiler for compilation. So we don't need to compile it manually.

     In direct programs the system looks for Shebang in order to execute a file.

     A shebang is the first line of the file. It tells which was the interpreter to be used for execution.

Python Code:


    A python code has an extension .py, For python the shebang will be either

#!/usr/bin/env python
or
#!/usr/bin/python

Create a file with your text editor as myfile.py

jhony@ljlinux:/home/programs$ vim myfile.py

Paste the code inside the file.

Code:
#! /usr/bin/python
print('Testing Program for Linux!!!')

Make the file an executable one:

Change the permission of the program file since it should executed.

jhony@ljlinux:/home/programs$ chmod +x myfile.py

Execute the file:

We can execute the file using ./ or calling the whole path of the file.

jhony@ljlinux:/home/programs$ ./myfile.py
Testing Program for Linux!!!

    If shebang is not mentioned in your code then you have to call the interpreter. There is no need of changing the file permission.

jhony@ljlinux:/home/programs$ python myfile.py
Testing Program for Linux!!!

Perl:


    A python code has an extension .pl, The shebang for perl will be either

#!/usr/bin/env perl
or
#!/usr/bin/perl

Create a file with your text editor as myfile.pl

jhony@ljlinux:/home/programs$ vim myfile.pl

Paste the code inside the file.

Code:
#!/usr/bin/perl

use strict;
use warnings;
print "Testing Program for Linux!!!\n";

Make the file an executable one:

Change the permission of the program file since it should executed.

jhony@ljlinux:/home/programs$ chmod +x myfile.pl

Execute the file:

Execute the file using ./ or calling the whole path of the file.

jhony@ljlinux:/home/programs$ ./myfile.pl
Testing Program for Linux!!!

    If shebang is not mentioned in your code then you have to call the interpreter. There is no need of changing the file permission as said earlier.

jhony@ljlinux:/home/programs$ perl myfile.pl
Testing Program for Linux!!!

Ruby:


    A ruby program has an extension .rb, The shebang for ruby is

#!/usr/bin/env ruby
or
#!/usr/bin/ruby

Create a file with your text editor as myfile.rb

jhony@ljlinux:/home/programs$ vim myfile.rb

Paste the below code inside the file.

Code:
#!/usr/bin/ruby

class Test
  def initialize
   puts "Testing Program for Linux!!!"
  end
end

# initialize object
Test.new

Make the file an executable one:

Change the permission of the program file since it should executed.

jhony@ljlinux:/home/programs$ chmod +x myfile.rb

Execute the file:

Execute the file using ./ or calling the whole path of the file.

jhony@ljlinux:/home/programs$ ./myfile.rb
Testing Program for Linux!!!

    If shebang is not mentioned in your code then you have to call the interpreter. 

jhony@ljlinux:/home/programs$ perl myfile.rb
Testing Program for Linux!!!

Bash(Shell):


    The bash program has an extension .sh, The shebang for bash script is #!/bin/bash

Create a file with your text editor as myfile.sh

jhony@ljlinux:/home/programs$ vim myfile.sh

Paste the below code inside the file.

Code:
#!/bin/bash

echo Testing Program for Linux!!!

Make the file an executable one:

Change the permission of the file.

jhony@ljlinux:/home/programs$ chmod +x myfile.sh

Execute the file:

Execute the file using ./ or calling the whole path of the file.

jhony@ljlinux:/home/programs$ ./myfile.sh
Testing Program for Linux!!!

    If shebang is not mentioned in your code then you have to call the interpreter. There is no need of changing the file permission.

jhony@ljlinux:/home/programs$ sh myfile.sh
Testing Program for Linux!!!

That's it for the day. On the upcoming articles we will see basic shell scripting with examples.

Feel free to ask if you have any questions.

Comments

  1. Be the first to add a comment.