emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org babel execute shell in sh?
@ 2012-03-07 17:25 Panruo Wu
  2012-03-08  0:22 ` Nick Dokos
  2012-03-08  4:24 ` Tom Regner
  0 siblings, 2 replies; 10+ messages in thread
From: Panruo Wu @ 2012-03-07 17:25 UTC (permalink / raw)
  To: emacs-orgmode

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

Dear list,

#+begin_src sh
for np in {1..32}
do
    echo $np
done
#+end_src

when executing, the output only shows
{1..32}
which is clearly not I want..

After some investigation, I found that orgmode
uses "sh" that cannot understand the for loop above.

My question is, how can I suggest orgmode to use
"bash" to execute shell script?

I tried :shebang #!/bin/bash but it does not work

Thanks,
robb

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

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

* Re: org babel execute shell in sh?
  2012-03-07 17:25 org babel execute shell in sh? Panruo Wu
@ 2012-03-08  0:22 ` Nick Dokos
  2012-03-08  4:24 ` Tom Regner
  1 sibling, 0 replies; 10+ messages in thread
From: Nick Dokos @ 2012-03-08  0:22 UTC (permalink / raw)
  To: Panruo Wu; +Cc: nicholas.dokos, emacs-orgmode

Panruo Wu <pwu@mymail.mines.edu> wrote:

> Dear list,
> 
> #+begin_src sh
> for np in {1..32}
> do
>   echo $np
> done
> #+end_src
> 
> when executing, the output only shows
> {1..32}
> which is clearly not I want..
> 
> After some investigation, I found that orgmode
> uses "sh" that cannot understand the for loop above.
> 
> My question is, how can I suggest orgmode to use
> "bash" to execute shell script?
> 
> I tried :shebang #!/bin/bash but it does not work
> 

Not a solution, just a workaround: you can write the loop
as

--8<---------------cut here---------------start------------->8---
#+begin_src sh
for np in $(seq 1 32)
do
   echo $np
done
#+end_src
--8<---------------cut here---------------end--------------->8---

Nick

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

* Re: org babel execute shell in sh?
  2012-03-07 17:25 org babel execute shell in sh? Panruo Wu
  2012-03-08  0:22 ` Nick Dokos
@ 2012-03-08  4:24 ` Tom Regner
  2012-03-09 20:34   ` Panruo Wu
  2012-03-10  4:21   ` Nick Dokos
  1 sibling, 2 replies; 10+ messages in thread
From: Tom Regner @ 2012-03-08  4:24 UTC (permalink / raw)
  To: Panruo Wu; +Cc: emacs-orgmode

Hi,

Panruo Wu <pwu@mymail.mines.edu> writes:

> Dear list,
>
>
> #+begin_src sh 
> for np in {1..32}
> do
>     echo $np
> done
> #+end_src
>
> when executing, the output only shows
> {1..32}
> which is clearly not I want..
>
> After some investigation, I found that orgmode
> uses "sh" that cannot understand the for loop above.
>
> My question is, how can I suggest orgmode to use
> "bash" to execute shell script?
>
> I tried :shebang #!/bin/bash but it does not work

I have the following in my config:

--------------------%<--------------------------
      I really like org-babel to use zsh
      #+begin_src emacs-lisp
        (setq org-babel-sh-command "zsh")
      #+end_src
--------------------%<--------------------------

It is apparently not possible to set this variable via #+BIND: to only
change this for one code block, at least I did not succeed to do so in
my attempts to do so -- but maybe I just didn't read enough of the
documentation to /get it right/.

I don't know, if zsh||bash instead of sh breaks any assumptions org-mode
makes about the environment in which sh code blocks are executed; up
until now it works like a charm.

Kind regards,
Tom

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

* Re: org babel execute shell in sh?
  2012-03-08  4:24 ` Tom Regner
@ 2012-03-09 20:34   ` Panruo Wu
  2012-03-10  4:21   ` Nick Dokos
  1 sibling, 0 replies; 10+ messages in thread
From: Panruo Wu @ 2012-03-09 20:34 UTC (permalink / raw)
  To: Tom Regner; +Cc: emacs-orgmode

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

Thanks tom! This is exactly what I need.

regards,
robb

On Wed, Mar 7, 2012 at 9:24 PM, Tom Regner <tom@goochesa.de> wrote:

> Hi,
>
> Panruo Wu <pwu@mymail.mines.edu> writes:
>
> > Dear list,
> >
> >
> > #+begin_src sh
> > for np in {1..32}
> > do
> >     echo $np
> > done
> > #+end_src
> >
> > when executing, the output only shows
> > {1..32}
> > which is clearly not I want..
> >
> > After some investigation, I found that orgmode
> > uses "sh" that cannot understand the for loop above.
> >
> > My question is, how can I suggest orgmode to use
> > "bash" to execute shell script?
> >
> > I tried :shebang #!/bin/bash but it does not work
>
> I have the following in my config:
>
> --------------------%<--------------------------
>      I really like org-babel to use zsh
>      #+begin_src emacs-lisp
>        (setq org-babel-sh-command "zsh")
>      #+end_src
> --------------------%<--------------------------
>
> It is apparently not possible to set this variable via #+BIND: to only
> change this for one code block, at least I did not succeed to do so in
> my attempts to do so -- but maybe I just didn't read enough of the
> documentation to /get it right/.
>
> I don't know, if zsh||bash instead of sh breaks any assumptions org-mode
> makes about the environment in which sh code blocks are executed; up
> until now it works like a charm.
>
> Kind regards,
> Tom
>

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

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

* Re: org babel execute shell in sh?
  2012-03-08  4:24 ` Tom Regner
  2012-03-09 20:34   ` Panruo Wu
@ 2012-03-10  4:21   ` Nick Dokos
  2012-03-10  6:33     ` Tom Regner
  1 sibling, 1 reply; 10+ messages in thread
From: Nick Dokos @ 2012-03-10  4:21 UTC (permalink / raw)
  To: Tom Regner; +Cc: Panruo Wu, emacs-orgmode, nicholas.dokos

Tom Regner <tom@goochesa.de> wrote:

> Hi,
> 
> Panruo Wu <pwu@mymail.mines.edu> writes:
> 
> > Dear list,
> >
> >
> > #+begin_src sh=C2=A0
> > for np in {1..32}
> > do
> > =C2=A0 =C2=A0 echo $np
> > done
> > #+end_src
> >
> > when executing, the output only shows
> > {1..32}
> > which is clearly not I want..
> >
> > After some investigation, I found that orgmode
> > uses "sh" that cannot understand the for loop above.
> >
> > My question is, how can I suggest orgmode to use
> > "bash" to execute shell script?
> >
> > I tried :shebang #!/bin/bash but it does not work
> 
> I have the following in my config:
> 
> --------------------%<--------------------------
>       I really like org-babel to use zsh
>       #+begin_src emacs-lisp
>         (setq org-babel-sh-command "zsh")
>       #+end_src
> --------------------%<--------------------------
> 
> It is apparently not possible to set this variable via #+BIND: to only
> change this for one code block, at least I did not succeed to do so in
> my attempts to do so -- but maybe I just didn't read enough of the
> documentation to /get it right/.
> 

You are right that it is not possible: #+BIND is effective only when you
are exporting - basically, people wanted to change various setting on
export, and adding options for each and every one was too much, so
Carsten implemented BIND as a general mechanism for that. But it does
not apply to anything else; in particular, code block evaluation is
completely separate.

But you can use general emacs mechanisms to set it for a particular file:
that's what file local variables are all about:

(info "(emacs)Local Variables in Files")

Nick

> I don't know, if zsh||bash instead of sh breaks any assumptions org-mode
> makes about the environment in which sh code blocks are executed; up
> until now it works like a charm.
> 
> Kind regards,
> Tom
> 

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

* Re: org babel execute shell in sh?
  2012-03-10  4:21   ` Nick Dokos
@ 2012-03-10  6:33     ` Tom Regner
  2012-03-12  4:34       ` Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: Tom Regner @ 2012-03-10  6:33 UTC (permalink / raw)
  To: nicholas.dokos; +Cc: Panruo Wu, emacs-orgmode



Nick Dokos <nicholas.dokos@hp.com> schrieb:

>Tom Regner <tom@goochesa.de> wrote:
>
>> Hi,
>>
>> Panruo Wu <pwu@mymail.mines.edu> writes:
>>
>> > Dear list,
>> >
>> >
>> > #+begin_src sh=C2=A0
>> > for np in {1..32}
>> > do
>> > =C2=A0 =C2=A0 echo $np
>> > done
>> > #+end_src
>> >
>> > when executing, the output only shows
>> > {1..32}
>> > which is clearly not I want..
>> >
>> > After some investigation, I found that orgmode
>> > uses "sh" that cannot understand the for loop above.
>> >
>> > My question is, how can I suggest orgmode to use
>> > "bash" to execute shell script?
>> >
>> > I tried :shebang #!/bin/bash but it does not work
>>
>> I have the following in my config:
>>
>> --------------------%<--------------------------
>>       I really like org-babel to use zsh
>>       #+begin_src emacs-lisp
>>         (setq org-babel-sh-command "zsh")
>>       #+end_src
>> --------------------%<--------------------------
>>
>> It is apparently not possible to set this variable via #+BIND: to
>only
>> change this for one code block, at least I did not succeed to do so
>in
>> my attempts to do so -- but maybe I just didn't read enough of the
>> documentation to /get it right/.
>>
>
>You are right that it is not possible: #+BIND is effective only when
>you
>are exporting - basically, people wanted to change various setting on
>export, and adding options for each and every one was too much, so
>Carsten implemented BIND as a general mechanism for that. But it does
>not apply to anything else; in particular, code block evaluation is
>completely separate.
>
>But you can use general emacs mechanisms to set it for a particular
>file:
>that's what file local variables are all about:
>
>(info "(emacs)Local Variables in Files")
>
>Nick
Ah, I didn't know that about BIND; but I do know file local variables. I don't need them in this case, as I always want to use zsh. And the OP asked for a different shell for one code block only, not the whole file.

But I'm often bitten by the distinction between export and tangling -- :padline, :shebang come to mind, where I expected org-babel to honour the setting in both cases.

well you live and learn: -) And org-mode truly is a joy to use, a marvelous piece of software, without which I couldn't do all my work im emacs.

Tom
>
>> I don't know, if zsh||bash instead of sh breaks any assumptions
>org-mode
>> makes about the environment in which sh code blocks are executed; up
>> until now it works like a charm.
>>
>> Kind regards,
>> Tom
>>


--
http://www.tomsdiner.de
xmpp: tom@sec.goochesa.de

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

* Re: org babel execute shell in sh?
  2012-03-10  6:33     ` Tom Regner
@ 2012-03-12  4:34       ` Eric Schulte
  2012-03-12 16:38         ` Tom Regner
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2012-03-12  4:34 UTC (permalink / raw)
  To: Tom Regner; +Cc: nicholas.dokos, emacs-orgmode, Panruo Wu

>
> But I'm often bitten by the distinction between export and tangling --
> :padline, :shebang come to mind, where I expected org-babel to honour
> the setting in both cases.
>

Could you describe a use case where these options would be used for
exporting and would be preferable to simply including the padding lines
or the shebang literally in the code block?

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

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

* Re: org babel execute shell in sh?
  2012-03-12  4:34       ` Eric Schulte
@ 2012-03-12 16:38         ` Tom Regner
  2012-03-12 17:29           ` Eric Schulte
  0 siblings, 1 reply; 10+ messages in thread
From: Tom Regner @ 2012-03-12 16:38 UTC (permalink / raw)
  To: Eric Schulte; +Cc: nicholas.dokos, emacs-orgmode, Panruo Wu

Eric Schulte <eric.schulte@gmx.com> writes:

>>
>> But I'm often bitten by the distinction between export and tangling --
>> :padline, :shebang come to mind, where I expected org-babel to honour
>> the setting in both cases.
>>
>
> Could you describe a use case where these options would be used for
> exporting and would be preferable to simply including the padding lines
> or the shebang literally in the code block?

I use an LP org-document with zsh-codefragments to generate a CLIF
testplan - the script is tangled, then executed and the output -- the
generated .ctp file -- exported. In this case I'd prefere it to have the
shell from the :shebang option used to run the tangled program, not
/bin/sh, so that exporting the output inside the document and running
the tangled program standalone produce the same result.

At the moment I have to set the shell document- or session-wide to zsh
to get reproducible behaviour.

Whenever the export/execution is part of the generated document and of
the generated product (the tangled code) I would like it, not to have to
sides to configure.

I don't know, if I make myself clear -- If not, please tell me so and I
try to distill an example out of the cases I encountered at work, where
I was wondering why some things did not work as I expected.

Kind regards,
Tom 

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

* Re: org babel execute shell in sh?
  2012-03-12 16:38         ` Tom Regner
@ 2012-03-12 17:29           ` Eric Schulte
  2012-03-12 19:10             ` Nick Dokos
  0 siblings, 1 reply; 10+ messages in thread
From: Eric Schulte @ 2012-03-12 17:29 UTC (permalink / raw)
  To: Tom Regner; +Cc: nicholas.dokos, emacs-orgmode, Panruo Wu

Tom Regner <tom@goochesa.de> writes:

> Eric Schulte <eric.schulte@gmx.com> writes:
>
>>>
>>> But I'm often bitten by the distinction between export and tangling --
>>> :padline, :shebang come to mind, where I expected org-babel to honour
>>> the setting in both cases.
>>>
>>
>> Could you describe a use case where these options would be used for
>> exporting and would be preferable to simply including the padding lines
>> or the shebang literally in the code block?
>
> I use an LP org-document with zsh-codefragments to generate a CLIF
> testplan - the script is tangled, then executed and the output -- the
> generated .ctp file -- exported. In this case I'd prefere it to have the
> shell from the :shebang option used to run the tangled program, not
> /bin/sh, so that exporting the output inside the document and running
> the tangled program standalone produce the same result.
>
> At the moment I have to set the shell document- or session-wide to zsh
> to get reproducible behaviour.
>
> Whenever the export/execution is part of the generated document and of
> the generated product (the tangled code) I would like it, not to have to
> sides to configure.
>
> I don't know, if I make myself clear -- If not, please tell me so and I
> try to distill an example out of the cases I encountered at work, where
> I was wondering why some things did not work as I expected.
>

I think I understand now, so it is "execution" not "export" behavior
which could make use of the :shebang header argument.  I suppose that it
would be possible to allow the :shebang header argument to silently
override the value of the `org-babel-sh-command' variable when it is
present.

I just pushed up a patch which adds this behavior.  It does result in
some odd new possibilities, such as the following.

#+begin_src sh :shebang #!/bin/cat
  foo
#+end_src

#+RESULTS:
| #!/bin/cat |
|            |
| foo        |

Best,

>
> Kind regards,
> Tom 

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

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

* Re: org babel execute shell in sh?
  2012-03-12 17:29           ` Eric Schulte
@ 2012-03-12 19:10             ` Nick Dokos
  0 siblings, 0 replies; 10+ messages in thread
From: Nick Dokos @ 2012-03-12 19:10 UTC (permalink / raw)
  To: Eric Schulte; +Cc: nicholas.dokos, Tom Regner, emacs-orgmode, Panruo Wu

Eric Schulte <eric.schulte@gmx.com> wrote:

> I just pushed up a patch which adds this behavior.  It does result in
> some odd new possibilities, such as the following.
> 
> #+begin_src sh :shebang #!/bin/cat
>   foo
> #+end_src
> 
> #+RESULTS:
> | #!/bin/cat |
> |            |
> | foo        |
> 

Maybe my settings are slightly different, but the new bits give me

,----
| #+begin_src sh :shebang #!/bin/cat
|   foo
| #+end_src
| 
| #+RESULTS:
| : foo
`----

They also give me sensible results with the original example:

,----
| #+begin_src sh :shebang #!/bin/bash
| for np in {1..32}
| do
|   echo $np
| done
| #+end_src
| 
| #+RESULTS:
| |  1 |
| |  2 |
| |  3 |
| |... |
| | 31 |
| | 32 |
`----

The only potentially confusing case I've found is the following:

,----
| #+begin_src sh
| #!/bin/bash
| for np in {1..32}
| do
|   echo $np
| done
| #+end_src
| 
| #+RESULTS:
| : {1..32}
`----

with the shebang as part of the script. I'd argue it's doing the right
thing however: if one remembers that sh is the default command, this is
equivalent to the command line invocation:

,----
| $ sh foo.sh
`----

where foo.sh contains

--8<---------------cut here---------------start------------->8---
#!/bin/bash
for np in {1..32}
do
  echo $np
done
--8<---------------cut here---------------end--------------->8---

and that too gives:

,----
| $ sh foo.sh
| {1..32}
`----

whereas

,----
| $ ./foo.sh
| 1
| 2
| 3
| ...
| 31
| 32
`----

In other words, sh does not interpret the shebang: that is only done
by the exec system call.

Nick

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

end of thread, other threads:[~2012-03-12 19:11 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-07 17:25 org babel execute shell in sh? Panruo Wu
2012-03-08  0:22 ` Nick Dokos
2012-03-08  4:24 ` Tom Regner
2012-03-09 20:34   ` Panruo Wu
2012-03-10  4:21   ` Nick Dokos
2012-03-10  6:33     ` Tom Regner
2012-03-12  4:34       ` Eric Schulte
2012-03-12 16:38         ` Tom Regner
2012-03-12 17:29           ` Eric Schulte
2012-03-12 19:10             ` Nick Dokos

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