[TriLUG] was more uselessinfo/commentary, now more percise perl question.
John F Davis
johndavi at us.ibm.com
Thu Mar 21 15:45:34 EST 2002
Hello
I am familar with HERE documents. Like I said, I did a similar thing in
BASH as what I am
doing in this perl script. Also, the HERE document below was meant to be
for the sfdisk
not perl.
For what it matters, Perl makes use of here documents. p67 of programming
perl,
perldoc -q HERE but it seems to only apply to stdout and not pipes to
programs.
man perlipc doesn't even cover HERE docs.
I'm rather disappointed that perl can't do this. This seems to be rather
simple.
JD
Andrew Perrin <andrew_perrin at unc.edu>@trilug.org on 03/21/2002 02:38:19 PM
Please respond to trilug at trilug.org
Sent by: trilug-admin at trilug.org
To: trilug at trilug.org
cc:
Subject: Re: [TriLUG] was more uselessinfo/commentary, now more percise
perl question.
I'm not entirely sure I understand what you're trying, but here goes:
> open(SFDISK, "|/sbin/sfdisk $drive << EOF") or die "Oops! $!";
^^^^^^
This doesn't do what you think it does (I suspect). What it does is
passes the string "<< EOF" to the shell for interpretation in executing
/sbin/sfdisk. The shell then interprets it as "execute /sbin/sfdisk with
the first argument the contents of $drive, and take input from the file
named EOF."
You just want to open an sfdisk process for writing:
open(SFDISK, "|/sbin/sfdisk $drive") or die "Oops! $!";
then write to it what you would type to it, using print():
print SFDISK "$bCyl, $eCyl, $partHash{qq{$drive$i}}, -\n";
...
then close the handle:
close SFDISK or die "Ouch! $!";
The EOF in my original suggestion was to allow you to send the data to the
filehandle without a lot of print statements; it's called a "HERE doc" in
perl, and allows you to do:
print <<HERE;
All of this information will be printed to STDOUT.
Variables like $i will be interpolated.
Perl code will not be executed. die "danger Will Robinson!" will only be
printed, not executed.
HERE
you should not print EOF (or, in this example, HERE) to the filehandle,
nor should you include it in the open() statement.
Finally, let me reiterate my recommendation of perldoc perlipc.
ap
----------------------------------------------------------------------
Andrew J Perrin - andrew_perrin at unc.edu - http://www.unc.edu/~aperrin
Assistant Professor of Sociology, U of North Carolina, Chapel Hill
269 Hamilton Hall, CB#3210, Chapel Hill, NC 27599-3210 USA
On Thu, 21 Mar 2002, John F Davis wrote:
> Hello
>
> Ok, this sucks, but I need your help again with this problem.
>
>
> # partition the disk.
> open(SFDISK, "|/sbin/sfdisk $drive << EOF") or die "Oops! $!";
> $cylCnt=$cyls/$numberOfPartitions;
> $bCyl=0;
> $eCyl=$cylCnt;
> for ($i = 0; $i < $numberOfPartitions; $i++) {
> $driveKey = $drive . (eval $i + 1);
> if ($eCyl == $cylCnt) {
> # print "hit the wrinkle.\n";
> $cylCnt--;
> }
> print SFDISK "$bCyl, $eCyl, $partHash{$driveKey}, -";
> print SFDISK "\n";
> # similar neat way: print $partHash{qq{$drive$i}};
> $bCyl = $eCyl;
> $eCyl = $eCyl + $cylCnt;
> }
> #print SFDISK "\n";
> print SFDISK "EOF\n";
> close SFDISK;
>
> # old bash way:
> # sfdisk $minidiskdev << EOF
> # 0,174,6,-
> # 174,174,83,*
> # 348,173,83,-
> # EOF
>
> This script is supposed to do what's in the above bash script. However,
it
> acts like its doing this:
> sfdisk /dev/hde << EOF
> EOF.
>
> (You can try this with your existing harddisk using sfdisk -n so it won't
> actually
> modify your partition table. You have been warned.)
>
> I played games with writing the "\n" and not. It doesn't seem to matter.
> Either way
> it doesn't work. man perllibc
>
> Any clues?
>
> Andrew Perrin <andrew_perrin at unc.edu>@trilug.org on 03/21/2002 12:07:02
PM
>
> Please respond to trilug at trilug.org
>
> Sent by: trilug-admin at trilug.org
>
>
> To: trilug at trilug.org
> cc:
> Subject: Re: was [TriLUG] perl programming question. now more
> uselessinfo/commentary
>
>
>
> Fair 'nuff, although the shell-script version doesn't handle either.
>
> ap
>
> ----------------------------------------------------------------------
> Andrew J Perrin - andrew_perrin at unc.edu - http://www.unc.edu/~aperrin
> Assistant Professor of Sociology, U of North Carolina, Chapel Hill
> 269 Hamilton Hall, CB#3210, Chapel Hill, NC 27599-3210 USA
>
>
> On Thu, 21 Mar 2002, Robert Wagoner wrote:
>
> > Andy,
> >
> > Small nit - to avoid possible problems when writing into a pipeline,
> > you should check the return status from the close() as well as the
> > open(), and you also need to handle a SIGPIPE if it occurs.
> > --
> > Robert (top-posting due to brain-damaged mailer)
> >
> >
> > >>> andrew_perrin at unc.edu 3/21/02 11:14 AM >>>
> > There are several ways to do this; I would probably do something like
> > the
> > following:
> >
> > open(SFDISK, "|/path/to/sfdisk /dev/hde") or die "Oops! $!";
> > print SFDISK <<EOF;
> > 0,174,6,-
> > 174,174,83, *
> > 348,173,83, -
> > EOF
> > close SFDISK;
> >
> > Best,
> > Andy (hoping he's right this time....)
> >
> > _______________________________________________
> > TriLUG mailing list
> > http://www.trilug.org/mailman/listinfo/trilug
> > TriLUG Organizational FAQ:
> > http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
> >
>
> _______________________________________________
> TriLUG mailing list
> http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ:
> http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
>
>
> _______________________________________________
> TriLUG mailing list
> http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ:
> http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
>
_______________________________________________
TriLUG mailing list
http://www.trilug.org/mailman/listinfo/trilug
TriLUG Organizational FAQ:
http://www.trilug.org/~lovelace/faq/TriLUG-faq.html
More information about the TriLUG
mailing list