Need return code from perl -e command

Discussion in 'Perl' started by Paul Porcelli, Jan 21, 2004.

  1. 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, Jan 21, 2004
    #1
    1. Advertising

  2. 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 <> wrote in message news:<bumhn3$i5v$>...
    > 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 %^|
    Paul Porcelli, Jan 22, 2004
    #2
    1. Advertising

  3. 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
    Carlos J. G. Duarte, Jan 23, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    799
    Keith Thompson
    Mar 6, 2005
  2. Ralph
    Replies:
    2
    Views:
    94
    Tad McClellan
    Aug 9, 2003
  3. Guy
    Replies:
    5
    Views:
    127
    Iain Chalmers
    Dec 2, 2003
  4. Replies:
    2
    Views:
    153
    Tad McClellan
    Mar 26, 2005
  5. Dan
    Replies:
    2
    Views:
    144
    John W. Krahn
    Aug 15, 2007
Loading...

Share This Page