I want to apply a unified diff from mypatch.diff
to stdin and output the result to stdout.
So far, I have tried:
patch -i mypatch.diff -o - -u originalfile
Which successfully applies mypatch.diff
and prints the result to stdout. However, I still have to provide the original file as originalfile
, not via stdin.
And if I try something like:
patch -i mypatch.diff -o - -u -
Then the patch gets rejected:
patching file -
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file -.rej
-o -
is not POSIX either, or at least AFAICT POSIX would require the output to go to a file called-
in the current working directory. – Stéphane Chazelas Mar 03 '23 at 18:16set -e
for anything but the simplest scripts, so typically not when functions of subscripts are used. I tend to prefer theset -o errexit
syntax. – Stéphane Chazelas Mar 03 '23 at 19:58mktemp -d
and use two separate tempfiles for input and output. You'd then delete the whole directory with both files and potential rej/orig files (I find that GNUpatch
saves rejects to-.rej
with-o -
) – Stéphane Chazelas Mar 03 '23 at 20:06set -o errexit
instead ofset -e
. Again, thank you very much for your feedback. – finefoot Mar 03 '23 at 23:27|| exit
overset -o errexit
here? If yes, just a personal preference or is there an argument for this choice? I feel like as I use the shell more and more, my scripts get a little more extensive and I add error handling and it helps to justset -o errexit
once at the beginning. – finefoot Mar 03 '23 at 23:32errexit
would be cancelled inpathstdin ... && echo Patch applied successfully
for instance. My rule is subshells or functions => no errexit – Stéphane Chazelas Mar 04 '23 at 07:35patch [options] [originalfile [patchfile]]
but it looks like you have the patch first. – alicederyn Mar 21 '24 at 09:09