emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Pascal Fleury <fleury@users.sourceforge.net>
To: Achim Gratz <Stromeko@nexgo.de>
Cc: emacs orgmode-mailinglist <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] ob-shell
Date: Wed, 21 Jan 2015 23:58:28 +0100	[thread overview]
Message-ID: <CA+p9wPA3q1LO5S4++KrKQRLUd2G4Wr=UQN+wHy0TrZNfcEdqgQ@mail.gmail.com> (raw)
In-Reply-To: <8761hke6ff.fsf@Rainer.invalid>


[-- Attachment #1.1: Type: text/plain, Size: 892 bytes --]

Hello,

Here is a patch that will figure out the version of bash in a less fork-y
way.
It keeps the result in a variable after having gotten it the first time by
indeed forking to bash.

--Pascal

On Fri, Aug 22, 2014 at 8:16 PM, Achim Gratz <Stromeko@nexgo.de> wrote:

> Pascal Fleury writes:
> > Please forget about the previous patch I submitted, I have a new one
> > that should work now on all platforms.
> > Let me know if it should be formatted differently.
>
> You don't really want to fork into bash each time you're about to run a
> shell code block just to find out if it suppports associative arrays.
> This needs to be determined at customization time.
>
> Regards,
> Achim.
> --
> +<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+
>
> Factory and User Sound Singles for Waldorf Q+, Q and microQ:
> http://Synth.Stromeko.net/Downloads.html#WaldorfSounds
>
>
>

[-- Attachment #1.2: Type: text/html, Size: 1451 bytes --]

[-- Attachment #2: 0001-PATCH-ob-shell.el-now-emits-associative-array-code-o.patch --]
[-- Type: application/octet-stream, Size: 3566 bytes --]

From 993c8e1da79c229a60aa76ade64780484a405df0 Mon Sep 17 00:00:00 2001
From: Pascal Fleury <fleury@google.com>
Date: Wed, 21 Jan 2015 23:55:34 +0100
Subject: [PATCH] [PATCH]ob-shell.el now emits associative array code only when
 the  installed version of bash supports them. This takes care of differences
 in  platforms that ship with bash v4+, that can deal with them, and those
 (e.g.  MacOSX) that ship with bash v3.x that does not support such arrays.

---
 lisp/ob-shell.el                   | 15 +++++++++++++--
 testing/examples/ob-shell-test.org | 10 ++++++++--
 testing/lisp/test-ob-shell.el      |  2 +-
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index aa14a69..48b38cc 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -38,6 +38,10 @@
 
 (defvar org-babel-default-header-args:sh '())
 
+;; Contains the version of bash that is installed. If nil, will be
+;; found by using bash itself once.
+(defvar org-babel-shell-bash-version nil)
+
 (defcustom org-babel-shell-names
   '("sh" "bash" "csh" "ash" "dash" "ksh" "mksh" "posh")
   "List of names of shell supported by babel shell code blocks."
@@ -121,12 +125,19 @@ This function is called by `org-babel-execute-src-block'."
      values
      "\n")))
 
+(defun bash-supports-assoc-arrays ()
+  "Returns if the current bash running on the system supports associative arrays."
+  (if (= org-babel-shell-bash-version nil)
+      (setq org-babel-shell-bash-version (org-babel-execute:shell "echo ${BASH_VERSINFO[0]}" nil)))
+  (> 3 org-babel-shell-bash-version)))
+
 (defun org-babel-variable-assignments:bash (varname values &optional sep hline)
   "Represents the parameters as useful Bash shell variables."
   (if (listp values)
-      (if (and (listp (car values)) (= 1 (length (car values))))
+      (if (or (not (bash-supports-assoc-arrays))
+	      (and (listp (car values)) (= 1 (length (car values)))))
 	  (org-babel-variable-assignments:bash_array varname values sep hline)
-	(org-babel-variable-assignments:bash_assoc varname values sep hline))
+	  (org-babel-variable-assignments:bash_assoc varname values sep hline))
     (org-babel-variable-assignments:sh-generic varname values sep hline)))
 
 (defun org-babel-variable-assignments:sh (params)
diff --git a/testing/examples/ob-shell-test.org b/testing/examples/ob-shell-test.org
index a54e5c0..ebd7421 100644
--- a/testing/examples/ob-shell-test.org
+++ b/testing/examples/ob-shell-test.org
@@ -80,9 +80,15 @@ echo ${table}
 Bash will see an associative array that contains each row as a single
 string. Bash cannot handle lists in associative arrays.
 #+begin_src bash :exports results :var table=sample_big_table
-echo ${table[spaghetti]}
+if (( ${BASH_VERSINFO[0]} > 3 )); then
+  # understands associative arrays
+  echo spaghetti ${table[spaghetti]}
+else
+  # Default V3.x behavior, has one array item contains the key and values.
+  echo ${table[1]}
+fi
 #+end_src
 
 #+RESULTS:
-: 20 cm
+: spaghetti 20 cm
 
diff --git a/testing/lisp/test-ob-shell.el b/testing/lisp/test-ob-shell.el
index 58a7859..9aa7f35 100644
--- a/testing/lisp/test-ob-shell.el
+++ b/testing/lisp/test-ob-shell.el
@@ -84,7 +84,7 @@ ob-comint.el, which was not previously tested."
   "Bash associative arrays as strings for the row"
   (org-test-at-id "82320a48-3409-49d7-85c9-5de1c6d3ff87"
     (org-babel-next-src-block 2)
-    (should (equal "20 cm" (org-babel-execute-src-block)))))
+    (should (equal "spaghetti 20 cm" (org-babel-execute-src-block)))))
 
 
 (provide 'test-ob-shell)
-- 
2.2.1


  reply	other threads:[~2015-01-21 22:58 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-22 10:03 2 Org tests failing Sebastien Vauban
2014-05-22 10:48 ` Bastien
     [not found]   ` <87tx8iks22.fsf-E3UqQZAQFPqWIDz0JBNUog@public.gmane.org>
2014-06-18 14:55     ` Sebastien Vauban
2014-06-18 21:49       ` Achim Gratz
2014-06-19  8:36         ` Sebastien Vauban
2014-06-19  9:04           ` Sebastien Vauban
2014-06-19  9:08             ` Sebastien Vauban
2014-06-19 20:05           ` Achim Gratz
2014-06-22  8:35             ` [PATCH] ob-shell (was: 2 Org tests failing) Achim Gratz
2014-06-22 12:50               ` [PATCH] ob-shell Eric Schulte
2014-06-23  6:26                 ` Pascal Fleury
2014-06-23 17:20                   ` Achim Gratz
2014-06-23 20:02                 ` Achim Gratz
2014-07-24  7:38                 ` Alan Schmitt
2014-08-04 11:56                   ` Eric Schulte
2014-08-04 19:48                   ` Achim Gratz
2014-08-13 13:12                     ` Alan Schmitt
2014-08-13 13:20                       ` Pascal Fleury
2014-08-13 13:29                         ` Alan Schmitt
2014-08-22  8:52                           ` Pascal Fleury
2014-08-22 13:08                             ` Pascal Fleury
2014-08-22 18:16                               ` Achim Gratz
2015-01-21 22:58                                 ` Pascal Fleury [this message]
2015-01-25 11:41                                   ` Achim Gratz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CA+p9wPA3q1LO5S4++KrKQRLUd2G4Wr=UQN+wHy0TrZNfcEdqgQ@mail.gmail.com' \
    --to=fleury@users.sourceforge.net \
    --cc=Stromeko@nexgo.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).