首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Emacs调用进程为sudo

Emacs调用进程为sudo
EN

Stack Overflow用户
提问于 2014-04-07 04:56:40
回答 2查看 984关注 0票数 2

更新

“我接受了”肖恩的回答,只是做了一些小小的修改。

代码语言:javascript
复制
(defun sudo-shell-command (buffer password command)
  (let ((proc (start-process-shell-command
           "*sudo*"
           buffer
           (concat "sudo bash -c "
                   (shell-quote-argument command)))))
    ;;; Added to @Sean answer to display the passed buffer
    (display-buffer buffer '((display-buffer . nil)) nil)
    (process-send-string proc password)
    (process-send-string proc "\r")
    (process-send-eof proc)))

(defun sudo-bundle-install (password)
  (interactive (list (read-passwd "Sudo password for bundle install: ")))
  (let ((default-directory (concat default-directory
                               "./fixtures/test-kitchen-mode-test-run/"))
    ;;; Added from accepted answer below by @sean
    ;;; need a buffer to display process in.
    (generated-buffer (generate-new-buffer "*test-kitchen-test-setup*")))
    (sudo-shell-command
      ;;; pass reference to generated process buffer by name.
      ;;; Need to add a defun to get the current test-kitchen buffer 
      ;;; if it exists to use, but that is outside the scope of the question.
      (buffer-name generated-buffer)
      password
      "bundle install; bundle exec berks install")
    (clear-string password)))

假设我需要用elisp调用一个过程,而这个过程需要sudo priveleges。例如,运行Ruby的bundle install

代码语言:javascript
复制
(let ((generated-buffer (generate-new-buffer "*test-kitchen-test-setup*")))
  (display-buffer generated-buffer '((display-buffer . nil)) nil)
  (call-process-shell-command
    (concat "cd " (concat default-directory "./fixtures/test-kitchen-mode-test-run") 
            "; sudo bundle install; sudo bundle exec berks install;")
    nil generated-buffer t))

bundle命令要求sudo正确安装gems。我如何用sudo在elisp中调用这个shell命令,输入密码,并且仍然能够在生成的窗口中显示结果?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-09 06:12:09

下面是一个助手函数,它使用提供的密码作为sudo执行单个shell命令:

代码语言:javascript
复制
(defun sudo-shell-command (buffer password command)
  (let ((proc (start-process-shell-command
               "*sudo*"
               buffer
               (concat "sudo bash -c "
                       (shell-quote-argument command)))))
    (process-send-string proc password)
    (process-send-string proc "\r")
    (process-send-eof proc)))

您可以将其应用于您的情况如下:

代码语言:javascript
复制
(defun sudo-bundle-install (password)
  (interactive (list (read-passwd "Sudo password for bundle install: ")))
  (let ((default-directory (concat default-directory
                                   "./fixtures/test-kitchen-mode-test-run/")))
    (sudo-shell-command
     "*test-kitchen-test-setup*"
     password
     "bundle install; bundle exec berks install")
    (clear-string password)))
票数 5
EN

Stack Overflow用户

发布于 2014-04-07 16:30:05

您需要用密码创建一个文件:

代码语言:javascript
复制
(defun my-run-command-under-sudo (password command &rest cpsc-args)
  "Run COMMAND under sudo with your PASSWORD.
Other arguments are passed to `call-process-shell-command'
 and should start with BUFFER (the output destination).
NB: this is _not_ secure: it creates a temp file with your password."
  (let ((password-file (make-temp-file "elisp-sudo")))
    (with-temp-file password-file (insert password))
    (unwind-protect
         (apply 'call-process-shell-command
                (concat "sudo " command)
                password-file cpsc-args)
      (delete-file password-file))))

现在你可以:

代码语言:javascript
复制
(let ((default-directory (concat default-directory "./fixtures/test-kitchen-mode-test-run")))
  (my-run-command-under-sudo "my-password" "bundle install" t)
  (my-run-command-under-sudo "my-password" "bundle exec berks install" t))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22903910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档