Commit 901f898203
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Babel.swift +3 −2
| @@ -411,10 +411,11 @@ public enum Babel { | ||
| 411 | 411 | let lines = shellAssignments(vars, shell: executable, params: params) |
| 412 | 412 | let cmdline = params.single[":cmdline"] |
| 413 | 413 | let shebang = params.single[":shebang"].flatMap { $0.contains(where: { !$0.isWhitespace }) ? $0 : nil } |
| 414 | if session == nil, stdin != nil || cmdline != nil || shebang != nil { | |
| 414 | if stdin != nil || cmdline != nil || (session == nil && shebang != nil) { | |
| 415 | 415 | // `org-babel-sh-evaluate`: an executable script, run by the shell with the |
| 416 | 416 | // command line after it and `:stdin` as its input; the result is its standard |
| 417 | // output, or its exit status for `:results value`. | |
| 417 | // output, or its exit status for `:results value`. `:stdin` and `:cmdline` | |
| 418 | // run it even with a session, which the script doesn't use. | |
| 418 | 419 | var script = (shebang ?? "#!/usr/bin/env \(executable)") + "\n" |
| 419 | 420 | if params.single[":padline"] != "no" { script += "\n" } |
| 420 | 421 | script += (lines + [body]).joined(separator: "\n") + (valueIsExitStatus ? "\necho $?" : "") |
Tests/OrgCoreTests/BabelTests.swift +6
| @@ -156,6 +156,10 @@ struct BabelTests { | ||
| 156 | 156 | "#+begin_src sh :results value :cmdline x\necho one\n#+end_src\n", |
| 157 | 157 | "#+begin_src bash :shebang #!/bin/bash :cmdline \"a b\" c :results output\necho $#\n#+end_src\n", |
| 158 | 158 | "#+begin_src sh :stdin src :results output\ntr a-z A-Z\n#+end_src\n\n#+NAME: src\n#+begin_src sh\necho hello\n#+end_src\n", |
| 159 | // `:stdin` and `:cmdline` run a script rather than the session. | |
| 160 | "#+begin_src sh :session s1 :cmdline a b :results output\necho $# $1\n#+end_src\n", | |
| 161 | "#+NAME: in\n| hello |\n\n#+begin_src sh :session s2 :stdin in :results output\ncat\n#+end_src\n", | |
| 162 | "#+begin_src bash :session s3 :shebang #!/bin/bash :cmdline x :var v=3\necho $v $1 ${BASH_VERSION:+bash}\n#+end_src\n", | |
| 159 | 163 | // `:var` from other blocks' results. |
| 160 | 164 | "#+begin_src sh :var x=five\necho $((x+1))\n#+end_src\n\n#+NAME: five\n#+begin_src sh\necho 5\n#+end_src\n", |
| 161 | 165 | "#+begin_src python :var t=tbl\nreturn [len(t), t[1][0]]\n#+end_src\n\n#+NAME: tbl\n#+begin_src python\nreturn [[1, 2], [3, 4]]\n#+end_src\n", |
| @@ -218,6 +222,8 @@ struct BabelTests { | ||
| 218 | 222 | (setq org-confirm-babel-evaluate nil |
| 219 | 223 | org-babel-python-command "python3" |
| 220 | 224 | default-directory temporary-file-directory) |
| 225 | ;; No shell session in batch; the cases with one don't use it. | |
| 226 | (advice-add 'org-babel-sh-initiate-session :override (lambda (s &rest _) (unless (member s '(nil "none")) s))) | |
| 221 | 227 | (org-babel-execute-src-block)) |
| 222 | 228 | """)) |
| 223 | 229 | } |