emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Problem with bash arrays from list-valued org-babel :var assignments?
@ 2017-06-09  4:56 Keith Amidon
  2017-06-09  9:16 ` Neil Jerram
  2017-06-09 22:53 ` Nicolas Goaziou
  0 siblings, 2 replies; 3+ messages in thread
From: Keith Amidon @ 2017-06-09  4:56 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: camalot

With current org-mode, when I try to execute the following org-babel
block:

#+begin_src bash :var lst='(1 2 3 4 5 6 7)
printf "%s\n" "${lst[*]}"
#+end_src

I get result and the following error in the minibuffer:

   Wrong type argument: listp, 1

This, on the other hand works fine:

#+begin_src emacs-lisp :var lst='(1 2 3 4 5 6 7)
lst
#+end_src

In investigating this, I looked into the code for how bash variables
are set from the org variables and found this code in ob-shell that I
don't really understand:

(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))))
	  (org-babel--variable-assignments:bash_array varname values sep hline)
	(org-babel--variable-assignments:bash_assoc varname values sep hline))
    (org-babel--variable-assignments:sh-generic varname values sep hline)))

I *think* that the org-babel--variable-assignments:bash_assoc function
is supposed to allow initializing bash arrays with arbitrary key/value
mappings when each element of the list is itself a list where the first
element of the sublist is the key of the mapping and the remaining
elements form the value, whereas the org-babel--variable-
assignments:bash_array function is supposed to create integer-indexed
bash arrays for simple lists.

However, if that is the case, the logic for picking between them
doesn't work as I would expect, since a simple list like the example at
the beginning of this email, '(1 2 3 4 5 6 7), will will fail the test
(listp (car values)) and result in the bash_assoc version being called
when the list is not of the correct form for that function.

The following version of the function that replaces the current test
with a simpler one seems to do what I think the expected behavior
should be for both types of lists, but it isn't clear to me whether the
current code is trying to support some other use cases that I don't
understand:

(defun org-babel--variable-assignments:bash (varname values &optional sep hline)
  "Represents the parameters as useful Bash shell variables."
  (if (listp values)
      (if (not (listp (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:sh-generic varname values sep hline)))

In any case, it seems like the simple example at the beginning of this
message should work. Is this a bug or am I missing something?

          Thanks, Keith

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

end of thread, other threads:[~2017-06-09 22:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-09  4:56 Problem with bash arrays from list-valued org-babel :var assignments? Keith Amidon
2017-06-09  9:16 ` Neil Jerram
2017-06-09 22:53 ` Nicolas Goaziou

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).