Other org babel tangle example

Does anyone have an example of an org babel tangle file for creating a shell script?

In case you have never come across org babel tangle, it is some emacs method of documenting a program as an org document and automatically creating and executable from it by some magic.

 
Does anyone have an example of an org babel tangle file for creating a shell script?
Add the following code to a file with a .org extension:
Code:
#+begin_src sh :results output
  echo PID: "$$"
#+end_src
Then place your cursor within the code and hit C-c C-c. You should see the result (the PID of your current process) output into a section in the same file called #+RESULTS:

More information and examples can be found at this page.

EDIT: Forgot to mention that you need to switch on Org Babel functionality by adding the following code to your init.el:
Code:
;; activate Babel languages
(org-babel-do-load-languages
'org-babel-load-languages
'((shell . t)))
 
Add the following code to a file with a .org extension:
Code:
#+begin_src sh :results output
  echo PID: "$$"
#+end_src
Then place your cursor within the code and hit C-c C-c. You should see the result (the PID of your current process) output into a section in the same file called #+RESULTS:

More information and examples can be found at this page.
I'm sure this will be useful once I get used to it, but at the moment it just looks like babble :)

EDIT: Forgot to mention that you need to switch on Org Babel functionality by adding the following code to your init.el:
Code:
;; activate Babel languages
(org-babel-do-load-languages
'org-babel-load-languages
'((shell . t)))
It looks like I don't need this in Doom emacs.

If you could post some more examples that would be useful.
 
Add the following code to a file with a .org extension:
Code:
#+begin_src sh :results output
  echo PID: "$$"
#+end_src
Then place your cursor within the code and hit C-c C-c. You should see the result (the PID of your current process) output into a section in the same file called #+RESULTS:

More information and examples can be found at this page.

EDIT: Forgot to mention that you need to switch on Org Babel functionality by adding the following code to your init.el:
Code:
;; activate Babel languages
(org-babel-do-load-languages
'org-babel-load-languages
'((shell . t)))
I'm just revisiting this and don't seem to be able to get the output sent to a file.

According to ChatGPT, if I want to send output to a file I should redirect the output in a command, ie

Code:
#+begin_src sh :results silent
echo "Hello, world!" > output.txt
#+end_src

Does that make sense?
 
That example does, but I thought
Code:
#+begin_src sh :results output
  echo PID: "$$"
#+end_src
the result would be placed in a file called 'output'.
Presumably that does not work.
 
Presumably that does not work.
No, that example doesn't produce an external file to the org file. That example should output the result of the code within the org file itself. As per the Org mode help file:
‘output’

Scripting mode. Org passes the code to an external process running the interpreter. Org returns the contents of the standard output stream as text results.

When using a session, Org passes the code to the interpreter running as an interactive Emacs inferior process. Org concatenates any text output from the interpreter and returns the collection as a result.
The reason that the other example worked as you expected is that you have explicitly redirected output into a file.
 
I have just discovered the use of the keyword tangle.

Code:
#+begin_src sh :tangle /babble-tangle-test
(message "Hello")
#+end_src

This simply puts the text between begin and end into the provide file.
 
Back
Top