Operating System - HP-UX
1827808 Members
2893 Online
109969 Solutions
New Discussion

Re: ksh scripting to perl

 
SOLVED
Go to solution
jerry1
Super Advisor

ksh scripting to perl

I am trying to write some perl code to do the
following from ksh script.


owner=`ll t | awk '{print $3}'`

cat data.file | mailx -s "Data" $owner


I am not having much luck and cannot find
any examples in the book to do the equivalent.





19 REPLIES 19
Alan Meyer_4
Respected Contributor
Solution

Re: ksh scripting to perl

Assuming that you have other perl processing to do besides this, it's simple enough to just allow the shell to do it inside the script.

$owner = `ls -l t |awk'{print $3}';
`cat data.file |mailx -s "Data" $owner`;

" I may not be certified, but I am certifiable... "
Hein van den Heuvel
Honored Contributor

Re: ksh scripting to perl

One of many ways (for a file called 'x'):


perl -e '$uid=(stat "x")[4]; $name=getpwuid $uid; open OU,"|mailx -s Data $name"; open IN,";'


Here I started the mail process through an open, piping data into it.

Alternatives are 'system()', fork, backtick,....

hth,
Hein.


jerry1
Super Advisor

Re: ksh scripting to perl

Alan, I started with just trying to put
in the commands as you described. But for
some reason the "awk" part does not get
executed and I end up with $owner being the
total line from the "ll" command and mailx
trying to send to each field listing from
the "ll" listing.


Hein van den, Your syntax does not show
any errors but it does not do anything.
I did change x to t for file name.
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting to perl

Hi Jerry:

I think Hein meant something like:

# perl -e '$x="/etc/hosts";$uid=(stat $x)[4];$name=getpwuid $uid;open OU,"|mailx -s Data $name";open IN,"<",$x;print OU $_ while ;'

...or slightly more generalized to use a filename as an argument:

# perl -e '$uid=(stat $ARGV[0])[4];$name=getpwuid $uid;open OU,"|mailx -s Data $name";open IN,"<",$ARGV[0];print OU $_ while ;' myfile

Regards!

...JRF...
Alan Meyer_4
Respected Contributor

Re: ksh scripting to perl

this works after testing

foreach $LINE (`ls -l *.dat`) {
chomp $LINE;
@LINE = split " ", $LINE;
`cat $LINE[8] |mailx -s Data $LINE[2]`;
}
" I may not be certified, but I am certifiable... "
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting to perl

Hi (again) Jerry & Hein:

Jerry, Hein's original solution DID WORK just fine if you merely substitute the name of your file for "x". My first cut-and-paste of it mangled it. The credit is his!

Regards!

...JRF...

/* no points please */
Hein van den Heuvel
Honored Contributor

Re: ksh scripting to perl


James is correct, I (over?) simplified the example to test for and send out the same file called "x". I assumed you needed changes anyway to pass in particular file names.

A little more clear perhpas by spreading it out in a script, and using your file names:
(untested)

$uid=(stat "t")[4];
$name=getpwuid $uid;
open OU,"|mailx -s Data $name";
open IN,"print OU $_ while ;

Hein.

jerry1
Super Advisor

Re: ksh scripting to perl

The reason:

'$uid=(stat "x")[4]; $name=getpwuid $uid; open OU,"|mailx -s Data $name"; open IN,";'

did not work for me is because of the begin
' and end '

When I took them out it worked.

It's back to the perl books. I have been
assimilated by ksh too long.

jerry1
Super Advisor

Re: ksh scripting to perl

Slowly I am piecing it together.

It runs but I am getting this error message
on one line. The line is:

my $name=getpwuid ($uid);

The error message is:

Use of uninitialized value in getpwuid at script1 line 33.

Why??

The script is enclosed if anyone wants to
use it, look at it. The code given by all
here was used in a script I wrote using the perl Net::FTP module.
It was the only was I could trap error messages.

H.Merijn Brand (procura
Honored Contributor

Re: ksh scripting to perl

$uid is not defined. You're sure to use the right syntax? Are you using strict and warnings? If not, you should.

#!/usr/bin/perl
use strict;
use warnings;

No script enclose as far as I can see

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
jerry1
Super Advisor

Re: ksh scripting to perl

Blast! Here is the script again here and
trying to enclose again.

#!/usr/bin/perl

use strict;
use warnings;
use Net::FTP;
use Net::Netrc;
my $server="host1";
my $file="data.file";
my $destination = "/";
my $pageid = "admins";
my $pager = "/usr/local/bin/pager";
my $ftpid = "user2";
my $pass = "xyzzy";

my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3) or die (exec "$pager $pageid \"$server: ftp AP cannot connect: $@\"") ;

$ftp->login("$ftpid","$pass") or die (exec "$pager $pageid \"ftp AP cannot login.\"") ;

$ftp->binary or die (exec "$pager $pageid \"ftp AP failed binary mode.\"") ;

$ftp->cwd ($destination) or die (exec "$pager $pageid \"ftp AP failed cwd.\"") ;

$ftp->put ($file) or die (exec "$pager $pageid \"ftp AP cannot put file. \"") ;

$ftp->quit;

my $date = "date +%Y%m%d%H%M";
my $uid=(stat "originalfile.$date")[4];
my $name=getpwuid ($uid);
open OU,"|mailx -s \"AP file sent to Citibank\" $name"; open IN,"data.file"; print OU $_ while ;



H.Merijn Brand (procura
Honored Contributor

Re: ksh scripting to perl

my $date = "date +%Y%m%d%H%M";
my $uid = (stat "originalfile.$date")[4];
my $name = getpwuid $uid;

if "originalfile.$date" does not exist, a stat () call on it will return undef in the user field, so maybe you will need to bail out if the file does not exist at all and then find out *why* it does not exist

my $date = "date +%Y%m%d%H%M";
my $org_file = "originalfile.$date";
-f $org_file or die "$org_file does not exist";
my $uid = (stat $org_file)[4];
my $name = getpwuid $uid;

or maybe you just wanted to stat () on $file instead

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
jerry1
Super Advisor

Re: ksh scripting to perl

Dang!

I took out %H%M from $date =
And touched a file ttt.`date +%Y%m%d`
But it still gives me the error:

Use of uninitialized value in getpwuid at aa line 33.

If I just touch the file name ttt and
change the code to only have ttt in:

my $uid = (stat "ttt")[4];

It works.

It has something to do with .$date in:

my $uid = (stat "ttt.$date")[4];

??

Does anyone know how to run debug with -d
-w. I can't seen to get anything meaningful
from it?

I can't get anything enclosed also in these
posts. I use Browse and open the file. It
shows G:\script1 in the attachment box but
does not get posted. Any clues for the clueless here?
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting to perl

Hi Jerry:

You need to use back-ticks to surround your date; not double quote marks. Thus:

my $ date = `date +%Y%m%d%H%M`;

Regards!

...JRF...
jerry1
Super Advisor

Re: ksh scripting to perl

James, tried that already. I get:

Unsuccessful stat on filename containing newline at script1 line 32.
Use of uninitialized value in getpwuid at script1 line 33.
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting to perl

Hi Jerry:

my $date = `date +%Y%m`;
chomp $date;
my $uid=(stat("/tmp/stuff.200509"))[4];

Note the 'chomp' of the newline delivered back from the 'date()'. That fixes stat() complaining.

Note the additional parentheses for the list context of the slice. That allows a value to be returned to $uid which then satisfies getpwuid().

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: ksh scripting to perl

Hi (again) Jerry:

Oops, forget my addition of the additional parenthesis with the stat() call. The 'chomp' fixes both of your errors!

Regards!

...JRF...

/* no points for this correction */
jerry1
Super Advisor

Re: ksh scripting to perl

And indeed it does work. I was wondering about the chomp.

Thanks James.
H.Merijn Brand (procura
Honored Contributor

Re: ksh scripting to perl

my $date = `date +%Y%m%d%H%M`;

is not very perlish

Perl has two native ways:

my @date = localtime;
my $date = sprintf "%4d%02d%02d%02d%02d", $date[5] + 1900, $date[4] + 1, @date[3,2,1];

or

use POSIX qw( strftime );
my $date = strftime ("%Y%m%d%H%M", localtime);

the first is completely native and most likely the fastest.
Neither needs chomp :)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn