emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Can I get the results of shell code block when exit code is non-zero?
@ 2019-08-29  7:09 Marcin Borkowski
  2019-08-29 16:13 ` Michaël Cadilhac
  0 siblings, 1 reply; 2+ messages in thread
From: Marcin Borkowski @ 2019-08-29  7:09 UTC (permalink / raw)
  To: Org-Mode mailing list

Hi all,

apparently when the exit code of the last command in a shell code block
is not zero, I do not get the results.  This doesn't seem to be
documented in the manual (though I might be missing something).  Check
this:

#+begin_src bash :results verbatim
  echo hello world
  exit 0
#+end_src

and this:

#+begin_src bash :results verbatim
  echo hello world
  exit 1
#+end_src

How can I tell Org to put the results in the file anyway?  My use case
is =diff=, which exists with status 1 if differences are found, and this
is a blog post, so I do not want to pollute the post with an =exit 0= at
the end of the code snippet.

TIA,

-- 
Marcin Borkowski
http://mbork.pl

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Can I get the results of shell code block when exit code is non-zero?
  2019-08-29  7:09 Can I get the results of shell code block when exit code is non-zero? Marcin Borkowski
@ 2019-08-29 16:13 ` Michaël Cadilhac
  0 siblings, 0 replies; 2+ messages in thread
From: Michaël Cadilhac @ 2019-08-29 16:13 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: Org-Mode mailing list

[-- Attachment #1: Type: text/plain, Size: 1531 bytes --]

Hi there,

This is the expected behavior, implemented in `org-babel-eval`. (By the
way, the docline of `org-babel-sh-evaluate` seems to talk about a different
function.)

As a workaround, you could define a new shell that always returns 0:

(push "0bash" org-babel-shell-names)
(org-babel-shell-initialize)
(defun org-babel-execute:0bash (body params)
  "Execute a block of bash commands with Babel, returning 0."
  (let ((shell-file-name "bash"))
    (org-babel-execute:shell (concat "trap 'exit 0' EXIT\n" body) params)))

—This is called 0bash because there are special treatments if the shell
name ends with "bash", see `org-babel--variable-assignments:bash`.

Cheers,
M.

On Thu, 29 Aug 2019 at 02:10, Marcin Borkowski <mbork@mbork.pl> wrote:

> Hi all,
>
> apparently when the exit code of the last command in a shell code block
> is not zero, I do not get the results.  This doesn't seem to be
> documented in the manual (though I might be missing something).  Check
> this:
>
> #+begin_src bash :results verbatim
>   echo hello world
>   exit 0
> #+end_src
>
> and this:
>
> #+begin_src bash :results verbatim
>   echo hello world
>   exit 1
> #+end_src
>
> How can I tell Org to put the results in the file anyway?  My use case
> is =diff=, which exists with status 1 if differences are found, and this
> is a blog post, so I do not want to pollute the post with an =exit 0= at
> the end of the code snippet.
>
> TIA,
>
> --
> Marcin Borkowski
> http://mbork.pl
>
>

[-- Attachment #2: Type: text/html, Size: 2133 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-29 16:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-08-29  7:09 Can I get the results of shell code block when exit code is non-zero? Marcin Borkowski
2019-08-29 16:13 ` Michaël Cadilhac

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).