#!/usr/bin/perl -w
#===============================================================================
#
# FILE: best_distro.pl
#
# USAGE: ./best_distro.pl
#
# DESCRIPTION: Finds best distro from distrowatch.com
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh)
# VERSION: 1.0
# CREATED: Tuesday 04 November 2008 04:05:19 IST
# REVISION: ---
#===============================================================================
use strict;
use warnings;
my $ndistro = 1;
if (@ARGV >= 1)
{
$ndistro = $ARGV[0];
if ($ndistro > 100)
{
$ndistro = 100;
}
}
my $lynx = `which lynx`;
chomp($lynx);
if ($lynx eq "")
{
print STDERR "$0: please install lynx\n";
print STDERR "sudo apt-get install lynx\n";
exit(-1);
}
open(LYNX, "lynx -source http://distrowatch.com/ |") or die("$0: cannot open lynx: $!\n");
my $i = 1;
while ($i <= $ndistro)
{
my $line;
my $distro;
my $rank = "";
while ($line = <LYNX>)
{
chomp($line);
$line =~ s/<th class=([^>]*)>(.*?)<\/th>/$2/i;
$line =~ s/(\s+)//g;
if ($line eq "$i")
{
$rank = $line;
unless ($line = <LYNX>) {last;}
chomp($line);
$line =~ s/<td class=([^>]*)><a href=([^>]*)>(.*?)<\/a><\/td>/$3/i;
$line =~ s/(\s+)//g;
$distro = $line;
print "$rank\t$distro\n";
last;
}
}
++$i;
}
close(LYNX);
Sample runs of above program:
$ ./best_distro.pl
1 Ubuntu
$ ./best_distro.pl 5
1 Ubuntu
2 openSUSE
3 Mint
4 Fedora
5 Debian
Wednesday, November 5, 2008
Finding Best Linux Distribution
Tuesday, November 4, 2008
Counts from min to max
I have written a shell script.
#!/bin/sh
if [ $# -ne 2 ]
then
echo "Usage: $0 min max"
exit
fi
min=$1
max=$2
while [ $min -le $max ]
do
echo -n $min " "
min=`expr $min + 1`
done
#end script
Sample run:
$ ~/bin/count.sh 2 5
2 3 4 5
Tip: This is very handy script, that is very helpful
in getting counts in the loop. For example:
$ for i in `~/bin/count.sh 2 8`
> do
> echo "Hello $i"
> done
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Friday, October 17, 2008
Total RAM in your System
$ cat /proc/meminfo | awk '/^MemTotal/ {print $2/1024 " MB"}'
2008.07 MB
If you all information related to memory:
$ cat /proc/meminfo
Extra: Total CPU cores in your system:
$ cat /proc/cpuinfo | awk '/^processor/ {ncores++} END {print "Total cores = " ncores}'
Total cores = 2
Thursday, October 16, 2008
Finding Processes, which are Accessing a File System
To view which files (of CD) are being accessed by whom :
$ fuser -mu /mnt/cd/*
If you are sure that, the processes are stuck/hang. Then you can kill all those processes :
$ fuser -kmu /mnt/cd/*
Warning!!!: If you are using writable medium (like USB stick), please give following command before killing processes:
$ sudo sync
Wednesday, October 15, 2008
Retrieving Date and Time from Unix Timestamp
Fri Aug 29 14:23:20 IST 2008
But in Linux, there is no '-r' option. Therefore, I have written a Perl script to
retrieve date and time from timestamp. Here is the script:
$ vi ~/bin/dater.pl
#!/usr/bin/perl -w
use strict;
use warnings;
my $ts = time;
if (@ARGV >= 1)
{
$ts = $ARGV[0];
}
my ($sec, $min, $hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
++$mon;
$year+=1900;
print "$mday-$mon-$year $hour:$min:$sec\n";
# end dater.pl
Sample runs of above program:
$ dater.pl
15-10-2008 12:34:37
$ dater.pl 1220000000
29-8-2008 14:23:20
PS: you can change parameters in print function in the program
according to output you desire.
Monday, October 13, 2008
Terminating Processes Containing Given Pattern
Generally, if you want to terminate a process which contains the given pattern, you use following 4 steps:
- $ ps aux | grep pattern
- Copy the pid of the process from output of above command.
- Paste this pid in the following command
- $ kill -9 pid
The above procedure is cumbersome and requires much typing.
Therefore I have written a program to terminate process(es), which contains given pattern. (I am assuming that ~/bin is in your $PATH).
$ vi ~/bin/kill_prog.sh
#!/bin/sh
# Kills the program given
if [ $# -lt 2 ]
then
echo "Usage: $0 [1/0] signal program-name"
echo "[0/1] Do not Kill / Do kill (optional)"
echo "signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)"
exit
fi
donothing=1
signal=$1
program=$2
if [ $# -gt 2 ]
then
donothing=$1
signal=$2
program=$3
fi
pids=$(ps auxww | awk "/ $program / { print \$2 }")
for pid in $pids
do
if [ $donothing -eq 1 ] ; then
echo "kill $signal $pid"
else
kill $signal $pid
fi
done
#end kill_prog.sh
By default, this program does not kills the process. You have to
pass 0 as second parameter to the command to kill.
Sample runs of above programs:
$ kill_prog.sh
Usage: /home/mitesh/Programming/Shell/WCS/kill_prog.sh [1/0] signal program-name
[0/1] Do not Kill / Do kill (optional)
signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)
$ kill_prog.sh -TERM ping
kill -TERM 16050
$ kill_prog.sh -KILL ping
kill -KILL 16050
$ kill_prog.sh 1 -KILL ping
kill -KILL 16050
$ kill_prog.sh 0 -KILL ping
Killed
The last command actually terminates(-TERM) or kills(-TERM).
Friday, October 3, 2008
Shell Session Recording
you can use script command. For example:
[mitesh@linuxbox:370:~]$ script
Script started, file is typescript
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit
Script done, file is typescript
You can view the output file typescript file using your favorite editor.
$ cat typescript
Script started on Friday 03 October 2008 04:51:27 PM IST
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit
Script done on Friday 03 October 2008 04:51:54 PM IST
PS: you can save the output to any file, you want instead of typescript in
current directory. For example
$ script shell_session_20081003.log
...
...
$ exit
$ cat shell_session_20081003.log
