1.

Solve : perl script for gathering info and sorting : query?

Answer»

below is a perl script which gathers the information about all the user’s files, sorts the result into numerical order and emails the user only the first 20 lines of the result.

any of the following commands to be made use of ( ls , sort , grep , wc , head , df , du )

#!/usr/bin/perl
# email a df report

open (GETSYS, "ls -al | sort -n | head -n 20 |") || die "Can’t read pipe\n";
open (MESSAGE, " |mail –s \"Disk Usage\" XXX\@somemailid.com")
|| die "No email\n";
while ($line = ) {
Print MESSAGE $line;
}


the script sends the user an email message containing information about their disk file utilisation. The mail message is sent to the specified address (XX[emailprotected])

will the above script do the job?
please let me know if my effort is right.

cheers!
Ray
you could do this too

Code: [Select]#!/usr/bin/perl
# email a df report
$CMD = qq(ls -al | sort -n | head -n 20 |mailx someid\@somedomain.com);
system($cmd);



Thanks ghostdog!
another one.. this ones abt pipes

#!/usr/local/bin/perl –w
# perl script to do a ls | grep “.pl” | wc –w
pipe PIPEIN1,PIPEOUT1;
pipe PIPEIN2,PIPEOUT2;
if (fork == 0)
{
close PIPEIN1;
close PIPEIN2;
close PIPEOUT2;
close STDOUT;
open STDOUT, “>&PIPEOUT1”;
exec “/bin/ls”;
}
if (fork == 0)
{
close PIPEIN2;
close PIPEOUT1;
close STDIN;
open STDIN, “<&PIPEIN1”;
close STDOUT;
open STDOUT, “>&PIPEOUT2”;
exec “grep .pl”;
}
if (fork == 0)
{
close PIPEIN1;
close PIPEOUT1;
close PIPEOUT2;
close STDIN;
open STDIN, “<&PIPEIN2”;
exec “/bin/wc -w”;
}
close PIPEIN1;
close PIPEOUT1;
close PIPEIN2;
close PIPEOUT2;
wait;
wait;
wait;

I'm supposed to modify the above program so that it can handle any three commands which MAY be piped together. The commands may be entered individually on the command line, one per line, so that there is no need to parse the line or identify the pipe symbol.

Is this for work? or is it an assignment/homework or some sort?
If its for work, why not do it in shell script? its easier

Code: [Select]#!/bin/sh
ls | grep “.pl” | wc –w

For example of everything done in Perl,
Code: [Select] while (<>) {
@words = split(/\W+/);
$count{$ARGV} += @words;
}
foreach $file ( keys %count) {
print "$file has $count{$file} words\n";
}

Reference: http://www.stonehenge.com/merlyn/UnixReview/col02.html

If still wish to implement pipes in Perl to call shell commands, just a simple idea...
Code: [Select]printf "Enter first command: ";
my $first=<STDIN>; chomp($first);
printf "Enter SECOND command: ";
my $second = <STDIN>; chomp($second);
printf "Enter third command: ";
my $third = <STDIN>; chomp($third);

my $cmd = join('|', $first,$second,$third) . "|" ;

open PIPE, "$cmd" or die "Couldn't open pipe, $!";
while(<PIPE>){print}
close PIPE;


thanks again! u r an ace ghostdog!

its an assignment to b done usin perl script only



Discussion

No Comment Found