1754043 Members
2747 Online
108811 Solutions
New Discussion юеВ

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.