sed: What does exit code 2 mean?

11,180

Solution 1

I'm not convinced that your problem is with sed. Is %BASH% supposed to evaluate to a directory in which the sed executable resides and have you confirmed that it does so? Because my expectation would be that (if defined at all on Windows) %BASH% would evaluate to the path to the bash executable itself, in which case appending "\sed" onto it makes no sense.

In particular I'll note that the DOS error code 2 is "File Not Found" - perhaps it's an issue with finding the file "%BASH%\sed"?

Solution 2

Does sed issue an exit code?

Most versions of sed do not, but check the documentation that came with whichever version you are using. GNU sed issues an exit code of 0 if the program terminated normally, 1 if there were errors in the script, and 2 if there were errors during script execution.

Source THE SED FAQ:

Share:
11,180

Related videos on Youtube

Michał Sacharewicz
Author by

Michał Sacharewicz

Updated on September 18, 2022

Comments

  • Michał Sacharewicz
    Michał Sacharewicz over 1 year

    Case

    I am trying to replace all occurrences of <value>*</value> into <value>file:\\</value> in a designated file test.txt.

    I work on Windows and use sed I have installed with Win-32 compiled set of bash utils.

    I use a following command:

    sed -n "s/<value>.*<\/value>/<value>file:\\\\<\/value>/g" "test.txt" > "test.txt.new"

    I want to run this command as a part of Wpkg script.

    Problem

    When I use this command from commandline, everything works properly.

    But when I use this command via Wpkg script:

    <install cmd='%BASH%\sed -n "s/&lt;value&gt;.*&lt;\/value&gt;/&lt;value&gt;file:\\\\&lt;\/value&gt;/g;" test.txt" > "test.txt new"' />
    

    ...then what I get is:

    Exit code returned non-successful value (2) on command '%BASH%\sed -n "s/<value>
    .*<\/value>/<value>file:\\\\<\/value>/g;" "test.txt" > "test.txt.new"'.
    

    The above means that sed application returned exit code 2. Additionaly, the new file has not been created.

    I browsed google for a really long time and to my surprise, sed seems to have a pretty poor documentation. I have found no documentation regarding this exit code.

    Question

    Anyone got a clue what does exit code 2 mean for sed?

    ...or an alternative solution?

    (must be unattended, pretty sure might use common bash tools)

  • Michał Sacharewicz
    Michał Sacharewicz about 9 years
    %BASH% is defined properly, but... "2" being a DOS error code, rather than sed's, is a great tip. I wonder if it might be the lack of .exe extension, that's really causing the problem :) I'll check soon, thanks!
  • fencepost
    fencepost about 9 years
    I've encountered problems when using Posix utils (e.g. 'find') on Windows from .bat/.cmd files, because the commands output and expect *nix-style paths. You don't appear to have that in your expression, but I've never used Wpkg - is it looking for *nix style paths even on Windows?
  • Michał Sacharewicz
    Michał Sacharewicz about 9 years
    If I were to run sed from within bash, I would have to use *nix style paths indeed. But, when run using cmd, standard Windows paths work properly.