By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,953 Members | 1,610 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,953 IT Pros & Developers. It's quick & easy.

Need return code from perl -e command

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 <bo****@fake.domain> wrote in message news:<bu**********@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 %^|

Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.