|
| From: | Max Nikulin |
| Subject: | Re: processing a literal example line by line in a shell source block? |
| Date: | Fri, 4 Nov 2022 10:15:25 +0700 |
| User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 |
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.
| [Prev in Thread] | Current Thread | [Next in Thread] |