How to Execute different programs in Linux - (C,C++,Java,Python,Perl,Bash,Ruby)

    
 
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:



For RPM machines:



Check the version:



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



Paste the below code inside the file.

Code:





Compiling:

    Compile the program using gcc command.





    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.


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.



Paste the code inside the file.

Code:





Compiling:

Compile the program using g++ command.



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





Executing:

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




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



Paste the below code inside the file.

Code:



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.





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



On other hand you can use a wild card operator * to compile all files



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.



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



Paste the code inside the file.

Code:



Make the file an executable one:

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



Execute the file:

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



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


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



Paste the code inside the file.

Code:



Make the file an executable one:

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



Execute the file:

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



    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.


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



Paste the below code inside the file.

Code:



Make the file an executable one:

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



Execute the file:

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



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


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



Paste the below code inside the file.

Code:



Make the file an executable one:

Change the permission of the file.



Execute the file:

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



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



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

Popular Posts on this blog

How to Install LAMP Stack on Linux - Ubuntu 17.04

Creating and Running Virtual Machines in LINUX - KVM - Cli Mode

Apache Webserver - Installation from source (tar.gz and tar.bz2)

Replacing Commands for Linux Files - Using Perl and Sed

How to install Docker on Ubuntu-17.04

Setting up a Mail server using Roundcube - Part-3 - Centos 7

Blog Top Sites

Blog Log