Saturday, November 2, 2013

How to login as root in your shell using bash script ?

Whenever I need to login as root in bash I use su command.
What if I need to login as root using a bash script?? After the command bash prompts for password.
I tried to write a bash script for the same but could not. I did some googling again got no solution. Then I remembered about a shell utility Expect. I installed it and wrote some simple scripts.
spawn su
expect "Password: "
send "rootpassword\r"
expect "#"
interact
With this script we are left logged into the root and we can run any bash commands there after.
I wrote the next one by mixing bash and expect.
#!/bin/bash
/usr/bin/expect << EOD
set timeout 120
spawn su
expect "Password: "
send "rootpassword\r"
expect "#"
EOD
echo "\r"
ls
But with this we are logged out (we can't interact in the shell as root) after completion of the script.

Friday, November 1, 2013

How to get top n and bottom n lines from a text file in a single pass ?

Some days ago, I came across a problem. The problem was to get the row-count , top n and bottom n number of lines from a text file in a single pass.
I thought for a while and remembered about a data-structure Queue that I had learned about in Data Structures and Algorithm course. I then wrote a simple Java code to get implementing Queue.
Below is the code I wrote, please read and give me feedbacks.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Test {
 private static Queue top=new LinkedList();
 private static Queue bottom=new LinkedList();
 private static int count=0;
 
 public static void main(String[] args) throws IOException{
  func(3);
  }
 
 //function to get count, top n, bottom n lines
 private static void func(int n) throws IOException{
  FileInputStream fstream = new FileInputStream("abc.txt");
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

  String strLine;

  //Read File Line By Line
  while ((strLine = br.readLine()) != null){
    count++;
    if(count<=n){
     top.add(strLine);    //initialize both top and bottom as top n 
     bottom.add(strLine);
    }else{
     bottom.remove();
     bottom.add(strLine);
        }
  }
  System.out.println(count);
  System.out.println(top.toString());
  System.out.println(bottom.toString());
  br.close();    
 }
}

Thursday, October 31, 2013

Download a file from repository using SVNKit

SVNKit is an opensource java SVN library. You can perform all the SVN tasks from Java program using this library.
I had to download a specific file from the repo but SVNKit didn't support file checkout. I googled a lot but couldn't find any specific solution (code snippet :D ). So, I tried writing it myself.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;


public class subversion {
public static void main(String[] args) throws SVNException, IOException{
 File newfile=new File("/home/everest/pom.xml"); 
 if (!newfile.exists()) {
  newfile.createNewFile();
 }
 FileOutputStream fop=new FileOutputStream(newfile);
 SVNURL url=SVNURL.parseURIEncoded("http://svn.svnkit.com/repos/svnkit/branches/1.1.x/");
 SVNRepository repository = SVNRepositoryFactory.create(url);
 repository.getFile("pom.xml", -1, null, fop);
 fop.flush();
 fop.close();
 System.out.println("Done!");
   }
}

I wasted my time searching the code. If I've written it then why shall you waste your time for the same reason ?? :P   

Friday, June 14, 2013

Cool Cowsay in your shell

Do you want cool cowsay as this when you open your shell???



If your answer if yes. Follow the steps below:

1) Install cowsay.
    You can do it by typing: sudo apt-get install cowsay 

2) Add following lines to your ~/.bashrc

if [ -x /usr/games/cowsay -a -x /usr/games/fortune ]; then
command fortune -a | fmt -80 -s | $(shuf -n 1 -e cowsay cowthink) -$(shuf -n 1 -e b d g p s t w y) -f $(shuf -n 1 -e $(cowsay -l | tail -n +2)) -n
fi

Have Fun!

Friday, March 8, 2013

A 3-D Cube

For the project of Computer Graphics, my team wrote a 3-D cube in HTML5 canvas and Javascript. We used basic algorithms of Computer Graphics to render a 3-D cube in the HTML5 canvas. The cube is rotated about Y-axis along the origin. Guess why did we choose this language? Because we could easily put it on the website and yes in my blog too :)

DigiLog

Your Browser doesnot support HTML5

Saturday, June 2, 2012

8085 Simulator


As a part of course in Computer Engineering we've to study about Intel 8085 processor. Not only theoretical but we also have to take practical class and completing the given practical assignment within the allocated time (during the class hours) is really tedious. So, here is the way out, a really cool open source 8085 Simulator available for both Linux as well as Windows platform. 

You can check the values of registers, check the output at ports and the view the values of different memory locations and many more (even I don't know all about it :P).

You can get your copy here: GNUSim8085

Thursday, April 19, 2012

Linus Torvalds: 2012 Millennium Technology Laureate


Technology Academy Finland today declared Linus Torvalds, the founder of Linux kernel as the 2012 Millennium Technology Prize Winner for the recognition of his new open source operating systems for computers.

This is a matter of great pride for all the LUGS (Linux User Groups).


The award is shared with Dr. Shinya Yamanaka for the recognition of his discovery of a new method to develop induced pluripotent stem cells for medical research.