emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Export arrays for 'sh' code blocks when using bash


From: Pascal Fleury
Subject: [O] Export arrays for 'sh' code blocks when using bash
Date: Thu, 27 Mar 2014 11:26:55 +0100

Hello,

I'dl like to propose a patch for inclusion into org-mode (ob-shell.el in particular).

TL;DR: use arrays and associative arrays when exporting variables to bash in 'sh' code blocks.

Details:
When variables are defined in a 'sh' code block, they are exported as strings. when the variable itself is an array or a table, then we simply get a shell variable that contains the list of all values in a non-structured form. E.g.

#+NAME: my_list
| one   |
| two   |
| three |

#+NAME: experiment
| name | first_attempt    |
| date | [2014-03-27 Thu] |
| user | fleury           |

#+BEGIN_SRC sh :var scalar="some value" :var array=my_list :var table=config
echo ${scalar}  # -> prints 'some value'
echo ${array}   # -> prints 'one two three'
echo ${table}   # -> prints 'first attempt [2014-03-27 Thu] fleury'
#+END_SRC

This will print simple strings. Also, there is no easy way to access the date of the experiment, for example. Now bash has things like arrays and associative arrays, but the current ob-shell.el does not use these. Probably because their syntax is bash-specific, and ob-shell.el is shell-agnostic.

My patch (attached) changes this in the case you have 
(setq org-babel-sh-command "bash")
in your emacs config somewhere. If any other value is used, it continues to export them as we do today (I don't know enough about other shells). 

In that case, it will export the list as an array, the table as an associative array, and the scalar as it does already. So the 'sh' code block could then use

#+BEGIN_SRC sh :var scalar="some value" :var array=my_list :var table=config
echo ${scalar}
echo ${array[1]} # -> prints "two"
echo ${table[user]} # -> prints "fleury"
#+END_SRC

In the case we have a bigger table, then the first row is used as key, the others are represented as a string with all the values (as it is for array currently). bash does not have multi-dimensional arrays, so it needs to be a string.
 
This makes writing shell snippets much easier in my experience, and there I'd like to propose this fix for the org-mode community at large.

--paf

Attachment: orgmode-bash.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]