Wednesday, April 8, 2009

Sending stderr (Standard Error) to Pipe

Many times we want to send output of standard error (stderr)
to pipe '|', so that we can use it as input for another command.

For example, while compilation of program, we are mostly interested
in error/warning messages. We can not get those lines by following
command:

$ make | egrep -i "error|warning"

so how can we can get only those error/warning message lines. We
just need to bind stderr (file handle 2) with stdout (file handle 1).
"2>&1"
The following example shows this:

$ make 2>&1 | egrep -i "error|warning"


Sunday, March 29, 2009

Backup Using rsync

There are many backup tools, but most are not as configurable as
rsync command. But still we may want more easiness to use rsync.
Therefore, I have written a Perl Script to use rsync for backing up
as many directories as you want. Here is a Perl script:


#!/usr/bin/perl
#===============================================================================
#
# FILE: rsync_backup.pl
#
# USAGE: ./rsync_backup.pl [output_dir]
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh), <mitesh[at]yahoo-inc[dot]com>
# VERSION: 1.0
# CREATED: Saturday 28 March 2009 07:36:54 IST IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

my $rsync_include = "rsync_include.conf";
my $rsync_exclude = "rsync_exclude.conf";
my $out_dir = "/media/FreeAgent Drive";
if (@ARGV >= 1)
{
$out_dir = $ARGV[0];
}

unless (-f $rsync_include)
{
print STDERR "$0: Include config file $rsync_include is not present.";
exit(-1);
}
my $exclude_option = "--exclude-from=$rsync_exclude";
unless (-f $rsync_exclude)
{
print STDERR "$0: exclude config file $rsync_exclude is not present.";
$exclude_option = "";
}
unless (-d $out_dir)
{
print STDERR "$0: $out_dir Destination dir does not exists.\n";
exit(-1);
}
$out_dir =~ s/ /\\ /g;

open(FH, "$rsync_include") or die("$0: Cannot open $rsync_include\n");
my $line;
my $nlines = 0;
while ($line = <FH>)
{
my $command;
my $options = "";
chomp($line);
++$nlines;
if ($line =~ /^#/) # skip comments
{
next;
}
my @fields = split(//, $line); # this junk char is Ctrl-X
if (@fields < 3)
{
print "Not enough arguments in line $nlines\n";
next;
}
if ($fields[2] =~ /^[Yy]/) # sync
{
$options .= " --delete --delete-excluded";
}
if (defined($fields[3]))
{
$options .= " $fields[3]";
}

$fields[0] =~ s/<home>/\/home\/mitesh/;
$fields[1] =~ s/<home>/\/home\/mitesh/;
$fields[0] =~ s/<out_dir>/$out_dir/g;
$fields[1] =~ s/<out_dir>/$out_dir/g;

$command = "rsync -av $fields[0] $fields[1] $options ";
system($command) == 0 or die("$0: Error in running\n$command\n$!\n");
}
close(FH);
exit(0);



There are 2 configuration files, I have used for this script. Namely
1. rsync_include.conf
2. rsync_exclude.conf

Please place these 2 files in the directory, from where you are going
to run this script, else you can change path of these configuration
files in rsync_backup.pl .

Sample rsync_include.conf contains:
# Configuration file for backing up
#
# ^X is delimiter, which can be typed as Ctrl-V Ctrl-X
#src_folder^Xdest_folder^Xdelete_at_dest[Y/n]^Xother_rsync_options
#
## delete_at_dest =>
# y = sync // Use cautiously
# n = copy blindly
#
# Some tags that can be used:
## <home> = Home directory; change to your home directory in the script.
## <out_dir> = Common Destination Directory
#
/home/mitesh/Music^X<out_dir>^Xy^X
<home>/Documents^X<out_dir>^Xn^X
<home>/Programming^X<out_dir>^Xn^X
/home/mitesh/Videos^X<out_dir>^Xn^X
#/workspace/temp/*^X<out_dir>/temp^Xn^X
/mnt/extra/Extra/Films/*^X<out_dir>/Videos^Xn^X
/mnt/extra/Extra/Download/*^X<out_dir>/Videos^Xn^X

Sample rsync_exclude.conf contains:
.trash
Trash
Cache
.thumbnails
*.torrent

Sample run (I have placed above config files in ~/bin/):
$ cd ~/bin
$ ./rsync_backup.pl
...
or, if you want to use some backup directory on harddisk,
mounted on /media/disk (say, /media/disk/backup)
$ ./rsync_backup /media/disk/backup

Thursday, March 26, 2009

Customizing Your Login Message With /etc/issue

If you are bored with same login message at Linux console,
you can change it by changing /etc/issue file.

# clear > /etc/issue
# echo "This is Mitesh's Machine." >> /etc/issue

Even you can provide other details of your system, by using
single alphabet given here.

# echo "\d \m \s \v" >> /etc/issue

Fortune Status for Pidgin IM

If you want to change status message of Pidgin Message by
fortune message. Just create a file pidgin_status_message.py
And copy the following lines.

#----------------------------

#!/usr/bin/env python

import dbus,subprocess,time

def set_status(message):
  bus = dbus.SessionBus()
  obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
  current = purple.PurpleSavedstatusGetType(purple.PurpleSavedstatusGetCurrent())
  status = purple.PurpleSavedstatusNew("", current)
  purple.PurpleSavedstatusSetMessage(status, message)
  purple.PurpleSavedstatusActivate(status)

while
True:
  fortune = subprocess.Popen('fortune', stdout = subprocess.PIPE).stdout.read()
  set_status(fortune)
  time.sleep(120)
#-------------------------------

Start Pidgin and give following command:
$ python pidgin_status_message.py &

Tuesday, January 13, 2009

Playing Directory in Mplayer

In order to play media files inside a directory with mplayer, I have written a shell script. Please save it in a directory in your $PATH variable.

For example, ~/bin/mplayer_dir.sh

#!/bin/bash


# Plays files inside a directory with mplayer

playlist="/home/mitesh/.mplayer/playlist.txt"

dir="./"
if [[ $# > 0 ]]
then
dir=$1
fi

#rm -f $playlist
find "$dir" -iname "*" | egrep -i "\.(wma|wmv|flv|mp3|avi|vob|dat|mp4|m4v|ogg|divx|xvid|rmvb|rm|asf)$" | sort > $playlist
#cat $playlist
nlines=`wc -l $playlist | awk '{print $1}'`

if [[ $nlines > 0 ]]
then
mplayer -playlist $playlist
else
echo "$0: Unrecognized files in $dir"
echo "Please update this script if playable format is present."

fi

--------------

Sample Run:

$ mplayer_dir.sh [directory]

Friday, November 28, 2008

Ping Every Host in /etc/hosts File.

If you want to know whether hosts specified in /etc/hosts file are reachable, just give the following command:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1

Sample run:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.028 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.028/0.028/0.028/0.000 ms


Friday, November 7, 2008

Missing Linux Entry in GRUB Menu

If any Linux entry is not present in GRUB menu,
you can visit following forum to view a solution.

Linux Forum: Unable to see Fedora 7 in GRUB Menu