emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* processing a literal example line by line in a shell source block?
@ 2022-11-02 18:17 Greg Minshall
  2022-11-03  7:41 ` Ihor Radchenko
  2022-11-04  3:15 ` Max Nikulin
  0 siblings, 2 replies; 5+ messages in thread
From: Greg Minshall @ 2022-11-02 18:17 UTC (permalink / raw)
  To: orgmode

hi.  i have a text in a named #+begin_example ... #+end_example block.
i would like to process this text line by line in a shell (bash, say)
code block.  but, it appears that the individual lines are not
separated, but passed as one long string to the source block.  (example
below.)

is there a magic incantation i can use to accomplish this?

cheers, Greg

#+name: lbl
#+begin_example
line 1
line 2
#+end_example

#+begin_src bash :var input=lbl :var in2='("first" "second")
  echo ${#input[@]}
  echo ${#in2[@]}
  echo ${input}
#+end_src

#+RESULTS:
|    1 |   |      |   |
|    2 |   |      |   |
| line | 1 | line | 2 |


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

* Re: processing a literal example line by line in a shell source block?
  2022-11-02 18:17 processing a literal example line by line in a shell source block? Greg Minshall
@ 2022-11-03  7:41 ` Ihor Radchenko
  2022-11-04  2:14   ` Greg Minshall
  2022-11-07  1:38   ` Greg Minshall
  2022-11-04  3:15 ` Max Nikulin
  1 sibling, 2 replies; 5+ messages in thread
From: Ihor Radchenko @ 2022-11-03  7:41 UTC (permalink / raw)
  To: Greg Minshall; +Cc: orgmode

Greg Minshall <minshall@umich.edu> writes:

> hi.  i have a text in a named #+begin_example ... #+end_example block.
> i would like to process this text line by line in a shell (bash, say)
> code block.  but, it appears that the individual lines are not
> separated, but passed as one long string to the source block.  (example
> below.)
>
> is there a magic incantation i can use to accomplish this?

Try

#+name: lbl
#+begin_example
line 1
line 2
#+end_example

#+begin_src bash :var input=lbl :results output
  echo "${input}"
#+end_src

#+RESULTS[719e61ee2f3fb87459530cf75b7bbef74b7d4337]:
: line 1
: line 2

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: processing a literal example line by line in a shell source block?
  2022-11-03  7:41 ` Ihor Radchenko
@ 2022-11-04  2:14   ` Greg Minshall
  2022-11-07  1:38   ` Greg Minshall
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Minshall @ 2022-11-04  2:14 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: orgmode

Ihor,

> Try
> 
> #+name: lbl
> #+begin_example
> line 1
> line 2
> #+end_example
> 
> #+begin_src bash :var input=lbl :results output
>   echo "${input}"
> #+end_src

ah, the double quotes!  thanks very much!

cheers, Greg


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

* Re: processing a literal example line by line in a shell source block?
  2022-11-02 18:17 processing a literal example line by line in a shell source block? Greg Minshall
  2022-11-03  7:41 ` Ihor Radchenko
@ 2022-11-04  3:15 ` Max Nikulin
  1 sibling, 0 replies; 5+ messages in thread
From: Max Nikulin @ 2022-11-04  3:15 UTC (permalink / raw)
  To: emacs-orgmode

On 03/11/2022 01:17, Greg Minshall wrote:
> hi.  i have a text in a named #+begin_example ... #+end_example block.
> i would like to process this text line by line in a shell (bash, say)
> code block.  but, it appears that the individual lines are not
> separated, but passed as one long string to the source block.  (example
> below.)
> 
> #+name: lbl
> #+begin_example
> line 1
> line 2
> #+end_example

You may use :stdin instead of :var, see 
https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-shell.html

#+begin_src bash :stdin lbl
   while read -r -a arr ; do
       printf 'value\t%s\n' "${arr[1]}"
   done
#+end_src

#+RESULTS:
| value | 1 |
| value | 2 |

> #+begin_src bash :var input=lbl :var in2='("first" "second")
>    echo ${#input[@]}
>    echo ${#in2[@]}
>    echo ${input}
> #+end_src

There is a nice tool: shellcheck. It should not be difficult to define a 
function that feeds current source block to it. The only point is to 
specify shell type since shebang is missed. In some cases even bash -n 
before running a script may save some time during debugging.





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

* Re: processing a literal example line by line in a shell source block?
  2022-11-03  7:41 ` Ihor Radchenko
  2022-11-04  2:14   ` Greg Minshall
@ 2022-11-07  1:38   ` Greg Minshall
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Minshall @ 2022-11-07  1:38 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: orgmode

Ihor,

again, thanks for helping me with that problem.

and, just for my (or anyone's) future reference, how i probably *should*
have debugged this was to tangle the relevant source block, and then
examine the resulting shell-script file, and gone from there.

cheers, Greg


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

end of thread, other threads:[~2022-11-07  1:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-02 18:17 processing a literal example line by line in a shell source block? Greg Minshall
2022-11-03  7:41 ` Ihor Radchenko
2022-11-04  2:14   ` Greg Minshall
2022-11-07  1:38   ` Greg Minshall
2022-11-04  3:15 ` Max Nikulin

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