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