emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Is it possible to pass variable to variables of src block?
@ 2017-12-05  5:19 Xi Shen
  2017-12-06  9:31 ` Michael Welle
  0 siblings, 1 reply; 5+ messages in thread
From: Xi Shen @ 2017-12-05  5:19 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

[-- Attachment #1: Type: text/plain, Size: 341 bytes --]

Hi,

http://orgmode.org/manual/var.html

This wiki explains how to use variable inside a src block. But I wonder if
it is possible to specify variable to the variables in the src definition.

E.g. for the sql src block, I want to execute a script on different server.
I want to define a variable for the ":dbhost" variable.


Regards,
David

[-- Attachment #2: Type: text/html, Size: 561 bytes --]

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

* Re: Is it possible to pass variable to variables of src block?
  2017-12-05  5:19 Is it possible to pass variable to variables of src block? Xi Shen
@ 2017-12-06  9:31 ` Michael Welle
  2017-12-12  6:30   ` Xi Shen
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Welle @ 2017-12-06  9:31 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Xi Shen <davidshen84@gmail.com> writes:

> Hi,
>
> http://orgmode.org/manual/var.html
>
> This wiki explains how to use variable inside a src block. But I wonder if
> it is possible to specify variable to the variables in the src definition.
>
> E.g. for the sql src block, I want to execute a script on different server.
> I want to define a variable for the ":dbhost" variable.
did you think about something like this?


(setq hmw/dbhost "db1")

#+BEGIN_SRC sql :dbhost (symbol-value 'hmw/dbhost) :dbuser weather :database environment :engine postgresql
select time, temperature from weather limit 20;
#+END_SRC

Regards
hmw

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

* Re: Is it possible to pass variable to variables of src block?
  2017-12-06  9:31 ` Michael Welle
@ 2017-12-12  6:30   ` Xi Shen
  2017-12-12  7:17     ` Ihor Radchenko
  2017-12-13  9:57     ` Michael Welle
  0 siblings, 2 replies; 5+ messages in thread
From: Xi Shen @ 2017-12-12  6:30 UTC (permalink / raw)
  To: Michael Welle; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1032 bytes --]

Hi Michael,

Yes, I think this is a way to my previous question. But what I really want
to achieve is to loop through a list of server names and execute a script
on them.

Say I have 20 different database server. How can I do a loop with each
server?


Regards,
David


On Wed, Dec 6, 2017 at 5:34 PM Michael Welle <mwe012008@gmx.net> wrote:

> Hello,
>
> Xi Shen <davidshen84@gmail.com> writes:
>
> > Hi,
> >
> > http://orgmode.org/manual/var.html
> >
> > This wiki explains how to use variable inside a src block. But I wonder
> if
> > it is possible to specify variable to the variables in the src
> definition.
> >
> > E.g. for the sql src block, I want to execute a script on different
> server.
> > I want to define a variable for the ":dbhost" variable.
> did you think about something like this?
>
>
> (setq hmw/dbhost "db1")
>
> #+BEGIN_SRC sql :dbhost (symbol-value 'hmw/dbhost) :dbuser weather
> :database environment :engine postgresql
> select time, temperature from weather limit 20;
> #+END_SRC
>
> Regards
> hmw
>
>

[-- Attachment #2: Type: text/html, Size: 1651 bytes --]

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

* Re: Is it possible to pass variable to variables of src block?
  2017-12-12  6:30   ` Xi Shen
@ 2017-12-12  7:17     ` Ihor Radchenko
  2017-12-13  9:57     ` Michael Welle
  1 sibling, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2017-12-12  7:17 UTC (permalink / raw)
  To: Xi Shen, Michael Welle; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2404 bytes --]

Hi David,

One thing you can do is to make a code block, which will generate
another code block, running your script with different arguments.
Let me illustrate with a simple example:

#+name: printecho
#+BEGIN_SRC sh :var a=1
  echo $a;
#+END_SRC

#+name: genscript
#+BEGIN_SRC sh :var cmd="printecho" :results raw output  :wrap "src sh :noweb yes :results output"
  echo "cat << EOF"
  for x in "1" "2" "3" "4" "5" "6" "something"; do echo "<<$cmd(a=\"$x\")>>"; done
#+END_SRC

#+RESULTS: genscript
#+BEGIN_src sh :noweb yes :results output
  cat << EOF
  <<printecho(a="1")>>
  <<printecho(a="2")>>
  <<printecho(a="3")>>
  <<printecho(a="4")>>
  <<printecho(a="5")>>
  <<printecho(a="6")>>
  <<printecho(a="something")>>
#+END_src

#+RESULTS:
: 1
: 2
: 3
: 4
: 5
: 6
: something

Hope it helps.

P.S. Does anyone know why the following does not evaluate noweb
recursively?

#+BEGIN_SRC sh :noweb yes :results output 
  <<genscript(cmd="printecho")>>
#+END_SRC

Regards,
Ihor

Xi Shen <davidshen84@gmail.com> writes:

> Hi Michael,
>
> Yes, I think this is a way to my previous question. But what I really want
> to achieve is to loop through a list of server names and execute a script
> on them.
>
> Say I have 20 different database server. How can I do a loop with each
> server?
>
>
> Regards,
> David
>
>
> On Wed, Dec 6, 2017 at 5:34 PM Michael Welle <mwe012008@gmx.net> wrote:
>
>> Hello,
>>
>> Xi Shen <davidshen84@gmail.com> writes:
>>
>> > Hi,
>> >
>> > http://orgmode.org/manual/var.html
>> >
>> > This wiki explains how to use variable inside a src block. But I wonder
>> if
>> > it is possible to specify variable to the variables in the src
>> definition.
>> >
>> > E.g. for the sql src block, I want to execute a script on different
>> server.
>> > I want to define a variable for the ":dbhost" variable.
>> did you think about something like this?
>>
>>
>> (setq hmw/dbhost "db1")
>>
>> #+BEGIN_SRC sql :dbhost (symbol-value 'hmw/dbhost) :dbuser weather
>> :database environment :engine postgresql
>> select time, temperature from weather limit 20;
>> #+END_SRC
>>
>> Regards
>> hmw
>>
>>

-- 
Ihor Radchenko,
PhD Student
Singapore University of Technology and Design,
8 Somapah Road Singapore 487372
Email: yantar92@gmail.com, ihor_radchenko@mymail.sutd.edu.sg
Tel: +6584017977

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: Is it possible to pass variable to variables of src block?
  2017-12-12  6:30   ` Xi Shen
  2017-12-12  7:17     ` Ihor Radchenko
@ 2017-12-13  9:57     ` Michael Welle
  1 sibling, 0 replies; 5+ messages in thread
From: Michael Welle @ 2017-12-13  9:57 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Xi Shen <davidshen84@gmail.com> writes:

> Hi Michael,
>
> Yes, I think this is a way to my previous question. But what I really want
> to achieve is to loop through a list of server names and execute a script
> on them.
>
> Say I have 20 different database server. How can I do a loop with each
> server?
interestingly I did not found a good solution after a bit of thinking...
The best I could come up with is something like this (which doesn't work):


#+name: foosql
#+BEGIN_SRC sql :dbuser weather :database environment :engine postgresql
select time, temperature from weather limit 10;
#+END_SRC


#+begin_src emacs-lisp :results raw :var hosts=(list "db1" "db2" "db3")
(dolist (host hosts)
  (org-sbe foosql (concat ":dbhost " host)))
#+end_src

Error: Symbol's definition is void: host. I guess that's caused by the
way the macro is evaluated. And the result, if any, of the sql query had
to be transformed back into a nice table for some use cases.

Using other means of calling a code block, like #+call etc., aren't
suitable for usage in a loop. So maybe someone can come up with a nice
solution ;).

Regards
hmw

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

end of thread, other threads:[~2017-12-13  9:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-05  5:19 Is it possible to pass variable to variables of src block? Xi Shen
2017-12-06  9:31 ` Michael Welle
2017-12-12  6:30   ` Xi Shen
2017-12-12  7:17     ` Ihor Radchenko
2017-12-13  9:57     ` Michael Welle

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