I define a global remote session for all my sh block code with properties
:PROPERTIES
:HEADER-ARGS:bash: :dir /ssh:xxx@myvm:/home/xxx/
:END:
In my other bash blocks, I inherit this property, but after working folder was created, my code are executed in sub folder of /home/xxx
Here an example with creation of two folders in /home/xxx
: todolist1
and todolist2
I finally found that noweb
is a good path to follow :
* Example
:PROPERTIES:
:HEADER-ARGS:bash: :dir /ssh:xxx@myvm:/home/xxx/
:END:
#+NAME: createdir
#+BEGIN_SRC bash :var folder="" :exports none
if [ -n "${folder}" ] ; then
if [ -d "${folder}" ]; then
rm -Rf "${folder}"
fi
mkdir "${folder}"
fi
#+END_SRC
#+NAME: gotodir
#+BEGIN_SRC bash :var folder="" :exports none
if [ -n "${folder}" ] ; then
cd "${folder}"
fi
#+END_SRC
#+BEGIN_SRC bash :var folder="todolist1" :noweb yes :exports both
<<createdir>>
#+END_SRC
#+BEGIN_SRC bash :var folder="todolist2" :noweb yes :exports both
<<createdir>>
#+END_SRC
// First action on todolist 1
#+NAME: addmytodolist1
#+BEGIN_SRC bash :var folder="todolist1" :noweb yes :exports both :results verbatim
<<gotodir>>
cat > todolist1.org <<EOF
#mytodolist
- [ ] item 1
- [X] item 2
EOF
#+END_SRC
// Second action on todolist 1
#+NAME: addmytodolist12
#+BEGIN_SRC bash :var folder="todolist1" :noweb yes :exports both :results verbatim
<<gotodir>>
cat >> todolist1.org <<EOF
- [ ] item 3
EOF
#+END_SRC
// First action on todolist 2
#+NAME: addmytodolist2
#+BEGIN_SRC bash :var folder="todolist2" :noweb yes :exports both :results verbatim
<<gotodir>>
cat > todolist2.org <<EOF
- [X] item 1
- [X] item 2
- [ ] item 3
- [X] item 4
EOF
#+END_SRC
To write less verbose and better maintainable code, i try without success :
to hide the
<<gotodir>>
instruction from export using:prologue <<gotodir>>
=> failedI also try to directly expand
dir:
into header args, but that doesn't take in account the remote TRAMP ssh connexion :#BEGIN_SRC sh :var folder="todolist1" :dir (expand-file-name (org-sbe "gotodir"))
I suppose it exist some elisp code to get TRAMP ssh folder, and rewrite it/overwrite it by using new url
{trampfolder}/todolist1
or{trampfolder}/todolist2
?
There are probably better and more robust way to do that, I'm interested to know what org-mode/org-babel guru :) !