emacs - 在 Emacs org-mode 中评估代码块时如何输入密码?

标签 emacs org-mode org-babel

我在 Org 文件中有一个 shell 代码块。里面有“sudo”命令。所以我应该被要求输入密码。但是当我评估它时,它只会返回“sudo”命令的错误。有什么方法可以使评估具有互动性吗?

最佳答案

是的,有,通过添加适当的 header arguments#+BEGIN_SRC线:

#+BEGIN_SRC sh :dir /sudo::
apt-get update
#+END_SRC

来源 : Running a sudo in a #+begin_src sh fails to get tty and askpass在 emacs-orgmode 邮件列表中。

解释 : :dir 争论

specifies the default directory during code block execution. If it is absent, then the directory associated with the current buffer is used.



通过 /sudo::作为 :dir 的值我们正在使用 TRAMP使用 super 用户权限访问文件或目录的语法。请注意,在上面的示例中,我们没有在 :: 之后指定要使用的目录。 .在这种情况下,/root将用作默认值。所以本质上,我们对 Org Babel 说的话是“以 root 身份访问 /root 并在执行此代码时将该位置用作默认目录”。

您可以通过更改 /sudo:: 来指定要使用的不同目录。到
/sudo::/path/to/dir

编辑

如果您需要代码块中的某些命令在没有 super 用户权限的情况下运行,您可以通过将
sudo -u <username>

在他们前面(将 <username> 替换为普通用户的用户名)。出于说明目的,假设您的用户名是 enchanter ,并且您希望代码块中不需要 super 用户权限的任何命令都以您自己的用户身份运行。在这种情况下,您必须为这些命令中的每一个添加前缀
sudo -u enchanter

您可以使用 whoami 来检查它是否有效。 (打印当前“事件”用户的名称)。添加
#+BEGIN_SRC sh :dir /sudo::
whoami
sudo -u enchanter whoami
whoami
#+END_SRC

给您的org-mode归档并评估它。输出将是:
#+RESULTS:
| root       |
| enchanter  |
| root       |

学分
  • This answer在 AskUbuntu
  • This answer在 super 用户

  • 我确信在 StackExchange/Internet 上还有其他帖子解决了以不同用户身份运行命令的问题,但我在找到这些后就停止了寻找......

    关于emacs - 在 Emacs org-mode 中评估代码块时如何输入密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621786/

    相关文章:

    Emacs 找不到 Flyspell/ispell

    emacs - 如何在组织模式下恢复稀疏 TreeView ?

    emacs - Emacs 可以区分 ctrl-r 和 ctrl-shift-r 吗?

    emacs - 创建 LaTeX 跳转到 PDF 的键绑定(bind) --synctex

    Emacs org-mode - 如何在不挂起 Emacs 的情况下使用后台进程运行 shell 脚本

    emacs - 如何在 org-mode 中逐行评估代码?

    common-lisp - SBCL 意外陷入浮点?

    emacs - 以不同样式导出 org-mode 代码块和结果

    c# - 使用 csharp-mode 和 Mono

    emacs - Jekyll 中使用 Org 模式的内部链接