Monday, 22 April 2013

lock process and allow the process to run after 1 mins

current=`date +"%s"`
last=`cat /tmp/last_date`
difference=`echo "$current - $last" | bc`
if [ ! -e /tmp/lock_file ] && [ $difference -gt 70 ]
then
touch /tmp/lock_file
sleep 10
echo "your command"
rm -rvf /tmp/lock_file
date +"%s" > /tmp/last_date
fi

Tuesday, 16 April 2013

greping particular range of lines in a file

open(FILE,"/etc/passwd");
$f=$ARGV[0];
$l=$ARGV[1];
while($line=<FILE>)
{
$d++;
if(($d>$f)&&($d<$l))
{
print $line."\n";
}
}
close(FILE);

usage: perl scriptname "line start" "endline"

Sunday, 14 April 2013

single if statement



print "Enter the password";
chomp ($s=<STDIN>);
$s eq "y" ? print "hi" : print "Bye" ;

Comparing File

use File::Compare;
if(compare("sam","sam1"))
{
print "file are same";
}
else
{
print "Files are not sare";
}

Changing Directory

chdir("/etc");
@s=readpipe(ls);
for($i=0;$i<@s;$i++)
{
push(@e,$s[$i]);
}
for($i=0;$i<@e;$i++)
{
print $e[$i]."\n";
}

Friday, 12 April 2013

Tuesday, 9 April 2013

Anchor in perl

#!/usr/bin/perl
open(FILE,"/etc/passwd");
while($line=<FILE>)
{
if ( $line =~ /ser\b/ )
{
print $line."\n";
}
}

Monday, 8 April 2013

Multithreading shell script

rm -rvf /tmp/lock
ls test/ | grep "sam[0-9]*" | tee /tmp/sam
proc()
{
if [ `ps aux | egrep -v 'watch|top|grep' | grep -i $0 | wc -l` -le 5 ]
then
$1 &
fi
}
funct()
{
until [ -e /tmp/lock ]
do
touch /tmp/lock
for i in `cat /tmp/sam`
do
cp -rvf test/$i test2/
sed -i "/$i/d" /tmp/sam
sleep 2
rm -rvf /tmp/lock
done
done
}
empt()
{
if [ `cat /tmp/sam | wc -l` -eq 0 ]
then
exit
fi
}
cat /tmp/sam | while read a
do
proc funct&
sleep 2
empt
done

Thursday, 4 April 2013

Find a file and rename th e file

use File::Find;
use File::Copy;
find(\&sam,"sam");
sub sam
{
copy("sam","samdev");
}

readpipe command

@s=readpipe(ls);
for($i=0;$i<@s;$i++)
{
if ( $s[$i] =~ /sam/ )
{
print $s[$i];
}
}

Perl redo

LINK: until ( $e eq "sam" )
{
&sa;
print Hi."\n";
redo LINK;
}

sub sa
{
chomp($e=<STDIN>);
}