2

I have an interesting problem that I am sure there must be a simple answer to if I can find the right syntax.

Essentially, as outlined below, I have a simple sh code block that prints a variable that is declared as part of #+begin_src.

#+name: test-fn
#+begin_src sh :var testvar="This is a test"
echo testvar=$testvar
#+end_src

#+RESULTS: test-fn
: testvar=This is a test

#+begin_src sh :noweb yes
<<test-fn>>
#+end_src

#+RESULTS:
: testvar=

When I execute the code block directly, the parameter is evaluated. When it is called from a noweb reference in a different code block, the parameter is not evaluated.

How can I get the called code block to evaluate it's parameters?

EDIT1: I am using GNU Emacs 25.0.94.2 (x86_64-w64-mingw32) of 2016-05-26 running on Windows 10

EDIT2: Including the actual problem in detail

#+name: test-harness-running?
#+begin_src ruby :results value
...
#+end_src

#+name: start-fw-test-harness
#+begin_src sh :noweb yes :var running=test-harness-running? :results output
...
#+end_src

#+name: remove-fw-flash
#+begin_src sh :results output
...
#+end_src

#+name: stop-fw-test-harness-if-running
#+begin_src sh :var running=test-harness-running? :noweb yes :results output
...
#+end_src

#+begin_src sh :noweb yes :results output
    <<stop-fw-test-harness-if-running>>
    <<remove-fw-flash>>
    <<start-fw-test-harness>>
#+end_src

The reason I've been doing it like this is I re-use stop-fw-test-harness-if-running, remove-fw-flash and start-fw-test-harness in many different scenarios (I used to use command line for it but I find babel gives me greater flexibility).

The problem is that start-fw-test-harness and stop-fw-test harness should only do their thing if the test harness is already started/stopped. However when called from a noweb reference, "running" never gets set as test-harness-running? is not called and so neither of these ever do anything. They work just fine when called directly.

EDIT3: Applying mutbuerger's solution to my problem, I get:

#+begin_src sh :noweb yes :results output :var dummy1=stop-fw-test-harness-if-running :var dummy2=remove-fw-flash :var dummy3=start-fw-test-harness
    echo $dummy1
    echo $dummy2
    echo $dummy3 
#+end_src

#+RESULTS:
: nil
: 22 Sep 2016 09:50:30 Deleting 351277001993561_STORE.prps 351277001993561_STORE.prps has been deleted
: nil

While this clearly works, I am reluctant to Accept the answer on account of the copious use of dummy variables and that I am not 100% sure the order in which the functions are called is clearly defined.

EDIT4:

Following mutbuerger's revised solution, this fixes my problem perfectly:

#+begin_src sh :results output :noweb yes
    <<stop-fw-test-harness-if-running()>>
    <<remove-fw-flash()>>
    <<start-fw-test-harness()>>
#+end_src
3

There may be other solutions, this is how I would do it:

#+begin_src sh :noweb yes :var res=test-fn()
echo $res
#+end_src

#+results:
: testvar=This is a test

The same can be done without introducing the dummy variable (as is described in the documentation):

#+begin_src sh :noweb yes
echo <<test-fn()>>
#+end_src

#+results:
: testvar=This is a test
  • Yes, thanks for the feedback mutbuerger, that works. However I've really dumbed down my problem to keep it simple - the reason I am using a noweb reference rather than the approach you've suggested is that I have a whole bunch of code blocks to execute. I'll put my actual problem in the question and I'd appreciate it if you could take a look and see how I can restructure it – Matt Innes Sep 22 '16 at 7:57
  • @MattInnes Updated the answer. – mutbuerger Sep 22 '16 at 10:02
  • 1
    Marvellous! Thanks! This is just the syntax breakthrough I was looking for - I spent ages looking at the manual yesterday (orgmode.org/manual/noweb.html#noweb) but it was clearly the wrong manual - I didn't know about the Org babel ref card so double thanks for enlightening me! – Matt Innes Sep 22 '16 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.