Linux

How To Get Started with Programming 101 in your GNU/Linux Distribution

vi

Before we get started with Programming in your favorite Linux distro, we need to familiarize text editors in Linux like ‘vi’ which is for advanced users, ‘nano’ and ‘gedit’ for beginners. For beginners, I recommend a command line text editor which is nano because it is simpler and its tutorial can be accessed by typing ‘man nano‘.

To install programming packages for Linux like python, perl, ruby, and gcc: type apt-get install <package_name>(Ex: apt-get install python) if you are using a Debian based distros like Ubuntu, BackTrack, BackBox, Nodezero, etc. If you plan to install it in an RPM-based system like Cent OS, Fedora Core, Red Hat, etc. : type yum install <package_name>.

In this tutorial we will only deal with the basics and so if you wanna learn advance programming of a specific language  then I suggest to search for more information/tutorials  in the web about the programming language of your taste.

C

To start with C, compiling is required so we need to install gcc : apt-get install gcc.

Here is a sample program I coded and named as sum.c:

#include <stdio.h>main() {
int num1;
int num2;
int total;/* This program will add two numbers */
printf(“Enter first number: n”);
scanf(“%d”, &num1);
printf(“Enter the 2nd number: n”);
scanf(“%d”, &num2);total = num1 + num2;
printf(“The sum is %d n”, total);
}

 

After we save it as sum.c, we need to compile it by typing: gcc sum.c -o sum

Now run it with this command : ./sum

gcc

Python

Compiling is not required if you want to program in python. Check out this simple login form using if and else statement:

#!/usr/bin/python
print “#########################################”
print “#”
print “#    Unauthorized Access Not Allowed”
print “#”
print “#########################################”
Username = raw_input(“LOGIN:”)
Password = raw_input(“PASSWORD:”)
if Username == “admin” and Password == “password”:
print “ACESS GRANTED”
else:
print “LOSER!”

Save this code as login.py , then make it executable with chmod +x password.py. To run the program type: ./login.py

python program
 

Perl

Just like python, to program in perl does not require compiling.  You can make a small program like this.

#!/usr/bin/perl
$lol = “e0wH p0Wh”;
print “hello is equivalent to Jejemon’s $lol n”;

Then make the file “hello.pl” executable by typing this command in the terminal :chmod +x hello.pl

You can run it with the command: ./hello.pl

perlprogramming

Ruby

To program in ruby, compiling is also not required. Now save this code as hi.rb

#!/usr/bin/ruby
dog  = “projectX”
blog = “blog readers”print “hello ”
puts dog
puts blog

Make the file executable : chmod +x hi.rb

Then, run it with this command in the terminal: ./hi.rb

rubyprogramming

Java

Before we can start running your Java programs, we need to compile it first by using javac command. Install OpenJDK 7 package in order to have javac command by typing: sudo apt-get install openjdk-7-jre / yum install openjdk-7-jre.

Here is a sample program:

class lol  {
public static void main (String [] args) {
      System.out.print(“I Love ProjectX Blog”);
}
}

Save the program as lol.java and then compile it by typing this command: javac lol.java

To run the program, type: java lol

javaprogramming

You might also want to try out this login form using javax.swing which I got from my subject in school about Data Structure Using Java:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class password {
private static String password = “pass”; /* password */
public static void main (String [] args) {
JFrame frame = new JFrame (“Password”);
frame.setVisible(true); /* Makes the Frame Appear on Screen */
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* set operation by default */
frame.setSize (500,200); /* size of the GU */

JLabel label = new JLabel (“Enter password”); /* display label */
JPanel panel = new JPanel ();
frame.add(panel);

JPasswordField pass = new JPasswordField (10);
pass.setEchoChar (‘*’);
pass.addActionListener (new AL ());
panel.add(label, BorderLayout.WEST);
panel.add(pass, BorderLayout.EAST);
}
static class AL implements ActionListener {
public void actionPerformed (ActionEvent e) {
JPasswordField input = (JPasswordField) e.getSource();
char [] passy = input.getPassword();
String p = new String (passy);

if (p.equals (password) ) {
JOptionPane.showMessageDialog(null, “Correct”);
}
else
JOptionPane.showMessageDialog (null, “Incorrect”);

}
}
}

javax.swing

Leave a Reply