Sunday, September 26, 2010

Shell

To remove duplicates in your .bash_history file, export this variable in the .bashrc file:
HISTCONTROL=erasedups

To increase the .bash_history size from 500 to 1000 commands, export this variable in the .bashrc file:
HISTSIZE=1000


alias hs='history | grep $1' 

Wednesday, July 14, 2010

Few interview questions.


Q1) Factorial of a large num ?
The main difficulty in computing factorials is the size of the result.


Constant Limit = 1000;           %Sufficient digits.
Constant Base = 10;              %The base of the simulated arithmetic.
Constant FactorialLimit = 365;   %Target number to solve, 365!
Array digit[1:Limit] of integer; %The big number.
Integer carry,d;                 %Assistants during multiplication.
Integer last,i;                  %Indices to the big number's digits.
Array text[1:Limit] of character;%Scratchpad for the output.
Constant tdigit[0:9] of character = ["0","1","2","3","4","5","6","7","8","9"];
BEGIN
 digit:=0;                       %Clear the whole array.
 digit[1]:=1;                    %The big number starts with 1,
 last:=1;                        %Its highest-order digit is number 1.
 for n:=1 to FactorialLimit do   %Step through producing 1!, 2!, 3!, 4!, etc. 
  carry:=0;                      %Start a multiply by n.
  for i:=1 to last do            %Step along every digit.
   d:=digit[i]*n + carry;        %The classic multiply.
   digit[i]:=d mod Base;         %The low-order digit of the result.
   carry:=d div Base;            %The carry to the next digit.
  next i;
  while carry > 0                %Store the carry in the big number.            
   if last >= Limit then croak('Overflow!'); %If possible!
   last:=last + 1;               %One more digit.
   digit[last]:=carry mod Base;  %Placed.
   carry:=carry div Base;        %The carry reduced.
  Wend                           %With n > base, maybe > 1 digit extra.
  text:=" ";                     %Now prepare the output.
  for i:=1 to last do            %Translate from binary to text.
   text[Limit - i + 1]:=tdigit[digit[i]]; %Reversing the order.
  next i;                        %Arabic numerals put the low order last.
  Print text," = ",n,"!";        %Print the result!
 next n;                         %On to the next factorial up.
END;

Monday, June 28, 2010

database operations

http://www.1keydata.com/sql/sql-create-index.html

 A  table index helps SQL statements run faster. The syntax for creating an index is:
CREATE INDEX IDX_CUSTOMER_LAST_NAME
on CUSTOMER (Last_Name)

 
 Copying table:
CREATE TABLE copyname SELECT * FROM originalname 

Creating key across multiple columns :
alter table tbl_name add primary key(col1,col2,col3);

If there are duplicates rows in a db across this combination:
alter ignore table judgments add unique index  `idx_nm` (col1,col2,col3);
and then drop the idx: alter ignore table judgments drop idx_name



Connections:
Q)How to check the number of connections to a db ?
A) show processlist;
 
Q)How to see the current indexes?
A) show indexes from table_name;

Thursday, June 3, 2010

View all files installed by a package with Debian

dpkg -l fortune : matches all packages with name containing fortune
dpkg -l fortunes-min: specifies files installed by package fortunes-min




Interesting commands:
cowsay , fortune ,toilet

Simple use of cut

ls -l | cut -d " " -f 1 : to print the permissions only
ls -l | cut -d " " -f2- : for the rest of fields

Nice post to detect ports.

http://www.redhat.com/docs/manuals/linux/RHL-9-Manual/security-guide/s1-server-ports.html
nmap -sT -O localhost
netstat -anp | grep 834 
lsof -i | grep 834