[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] Is it possible to pass variable to variables of src block?
From: |
Ihor Radchenko |
Subject: |
Re: [O] Is it possible to pass variable to variables of src block? |
Date: |
Tue, 12 Dec 2017 15:17:11 +0800 |
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 <address@hidden> 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 <address@hidden> wrote:
>
>> Hello,
>>
>> Xi Shen <address@hidden> 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: address@hidden, address@hidden
Tel: +6584017977
signature.asc
Description: PGP signature