Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl (http://www.velocityreviews.com/forums/f17-perl.html)
-   -   Need return code from perl -e command (http://www.velocityreviews.com/forums/t24768-need-return-code-from-perl-e-command.html)

Paul Porcelli 01-21-2004 12:39 PM

Need return code from perl -e command
 
Hi folks,
I have a perl one-liner embedded in a ksh script.

perl -pi.bak -e "s/val/otherval/" inputfile

I'd like to check the return code to know
if the substitution was successful.

If I type:
# perl -pi.bak -e "s/val/otherval/" inputfile
Can't open inputfile: No such file or directory
# echo $?
0

If the file is found i still get a return code of 0
# perl -pi.bak -e "s/val/otherval/" realfile
# echo $?
0

Any ideas ?

Many thanks
Paul

Paul Porcelli 01-22-2004 09:26 AM

Re: Need return code from perl -e command
 
Hi Alex,
thanks a lot for the reply.
That command doesn't seem to work for me.
It's partially OK if the string is found
i.e.
# perl -pi.bak -e '$c+=s/test/newval/; END {exit !$c;}' /tmp/testfile
# more /tmp/testfile
0newval
N.B. The return code 0 has been pasted into the file.
# echo $?
0

If there are no substitutions:
# perl -ne '$c+=s/echo/newval/; END {exit !$c;}' /tmp/testfile
Callback called exit, <> chunk 1.
END failed--cleanup aborted, <> chunk 1.
# echo $?
255

Any ideas how to avoid putting the return code in the file ?

Thanks again
Paul

Chillies <bounce@fake.domain> wrote in message news:<bumhn3$i5v$1@newsg4.svr.pol.co.uk>...
> Paul Porcelli wrote:
> > Hi folks,
> > I have a perl one-liner embedded in a ksh script.
> >
> > perl -pi.bak -e "s/val/otherval/" inputfile
> >
> > I'd like to check the return code to know
> > if the substitution was successful.

>
> The s// command returns the number of characters
> substituted, and you can use exit fro the retval.
>
> perl -ne '$c+=s/val/newval/; END {exit !$c;}'
>
> returns 0 if successful, 1 if not.
>
> > Paul

>
> Alex
> Sorry, my newsfeed doesn't have comp.lang.perl %^|


Carlos J. G. Duarte 01-23-2004 12:49 AM

Re: Need return code from perl -e command
 
Paul Porcelli wrote:
> Hi folks,
> I have a perl one-liner embedded in a ksh script.
>
> perl -pi.bak -e "s/val/otherval/" inputfile
>
> I'd like to check the return code to know
> if the substitution was successful.
>


Hi, I was replying to this the other day, when my laptop crashed! Let's
try take two now...

Perl returns the result of the last evaluated expression. perl -p is
equivalent to include all code between a while (<>) {}, so if the file
exists it enters the while loop, does it's work, and returns when <>
returns undef. Therefor here the last evaluated expression is <> that
have returned undef (or something) -- perl exits 0. When the file does
not exist, the <> implicitly returns undef again (open on a non existent
file).

To actually have a different return code based on taking or not some
action, one must consider that case in the script. Two examples:

#1 exit via the $! perl var
perl -i -p -e '$!=0; s/foo/bar/ END { return !!$! }' files

#2 take some var to control the number of processed lines
perl -i -p -e '$c++; s/foo/bar/ END { return !$c }' files

Both has some problems, for instance, the first will return false if the
lsat file does not exists (independently of the first and second, etc);
the later will return false if no file has lines.

--
carlos ** http://cgd.sdf-eu.org


All times are GMT. The time now is 08:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57