Shell Script to Kill Process(number of process) Unix/Solaris

bash-2.05$ processkill
Usage: ./processkill [-p|-f] [-u username] [num] regex 
Kill processes matching a regular expression 
-p: print processes matching regular-expression, don’t kill 
-f: force (don’t ask) 
num: send specified number to process. 15 if none given

 

This is the Script.
———————————–

#!/usr/bin/perl -w
#
# Kill processes matching a regular expression

# TODO:
# -Use perl function to access process managment instead of @ps=`ps …`

my $force=0;
my $print_only=0;
my $user_name=””;
my $regex;

if($#ARGV==-1 || $ARGV[0]=~/-h/){
print “Usage: $0 [-p|-f] [-u username] [num] regexn” .
” Kill processes matching a regular expressionn” .
” -p: print processes matching regular-expression, don’t killn” .
” -f: force (don’t ask)n” .
” num: send specified number to process. 15 if none givenn”;
exit;
}

if($ARGV[0]=~/^-p/){
$print_only=1;
shift(@ARGV);
}
elsif($ARGV[0]=~/^-f/){
$force=1;
shift(@ARGV);
}

if($ARGV[0]=~/^-u/){
shift(@ARGV);
$user_name = shift(@ARGV);
# print”*** $user_namenn”;
}

$signal=15;

if($ARGV[0]=~/^d*$/){
$signal=$ARGV[0];
shift(@ARGV);
}

$regex=$ARGV[0];

#@ps=`ps –no-header -Ao pid,args`;
if ($user_name)
{
@ps=`ps -u $user_name -o pid,args`;
}
else
{
@ps=`ps -Ao pid,args`;
}

$kill_apps=”kill -$signal “;
$num2kill=0;
shift @ps;
foreach $ps (@ps){
$ps =~ /^s*(d*)s*(.*)$/;
$pid=$1; # process-ID
$command=$2;
if($command=~/$regex/ && $$!=$pid){
if($print_only or !$force){
print “$pid $commandn”;
}
$kill_apps.=”$pid “;
$num2kill++;
}
}

if(!$print_only && $num2kill!=0){
$answer=”y”;
if($force==0){
print “Kill these processes? (y|n)”;
$answer=<STDIN>;
}
if($answer=~/^y/ or $answer=~/^Y/){
print $kill_apps;
print `$kill_apps 2>&1`;
print “n”;
}
else{
print “No processes killedn”;
}
}

———————————–

 

I parsed your code and practically everything seemed to be correct.
I feel you there is something wrong with the interpreter line.
check the path of your perl binary aka #which perl
so the first line should be
#!<the_actual_path_that_is_returned_by_which perl>
probably in your earlier env you would have been having this path
/usr/bin/perl
but perl is installed in some other dir.
correct the first (interpreter line ) as stated above and your code will run fine.