13

I want to execute sudo command in org-babel like this:

#+begin_src shell :dir "/sudo::" :cache no
make
sudo make install
#+end_src

It works fine. It use TRAMP /sudo: method.

But I need to execute sudo command relative under current working directory. As following block shows:

#+begin_src shell :dir "/sudo::data/code/quirc" :cache no
make
sudo make install
#+end_src

But it does not work. It is under /root instead of current working directory. So how can I execute sudo command under a specific directory as in org-babel like upper?

EDIT: Might add an advice to change TRAMP sudo method default directory to babel src block's directory? Don't know how to change this in TRAMP code. Have not found any tramp-default-directory related variables.

stardiviner
  • 1,888
  • 26
  • 45

2 Answers2

7

You must use an absolute local directory path. Like this:

#+begin_src shell :dir "/sudo::/data/code/quirc" :cache no
make
sudo make install
#+end_src

See the leading slash in /data/code/quirc.

Edit: You must recompute the directory for the root shell. Something like this:

#+begin_src shell :dir "/sudo::" :var dir=(expand-file-name "data/code/quirc") :cache no
cd $dir
make
make install
#+end_src
Michael Albinus
  • 6,647
  • 14
  • 20
5

I suddenly come up with string concat idea.

Here is the very simple solution.

#+begin_src sh :dir (concat "/sudo::" (expand-file-name "data/code"))
pwd
#+end_src

#+RESULTS[(2020-07-20 10:34:50) b20fc4ca99ae523b3497adaa26b8945c57b06254]:
: /home/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/Data/Emacs Packages/Org Mode/data/code
stardiviner
  • 1,888
  • 26
  • 45
  • This is a better solution than mine above. It's cleaner not requiring a local variable. It seems that keeping the current directory should be the default behavior. – Yu Shen Jul 21 '20 at 04:05
  • I original indeed have this idea. But I found it's not a good idea. I digged the ob-core.el source code, it's not good to keep current default directory for remote path like tramp method path. For example `:dir /ssh:user@host:data/code` Like this remote path should not keep default directory. There is a complex solution, add a new header argument or emacs defcustom option to control this behavior. But it's not trivial. So currently my solution should be the simpiest and elegant now. – stardiviner Jul 22 '20 at 04:29