更新
我接受了@Sean的答案,并进行了一些小修改。
(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
:
(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
才能正确安装gem。如何在elisp中使用sudo调用此shell命令,输入密码,并仍然能够在生成的窗口中显示结果?
答案 0 :(得分:4)
这是一个辅助函数,它使用提供的密码以sudo的形式执行单个shell命令:
(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)))
您可以将它应用于您的情况:
(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)))
答案 1 :(得分:0)
您需要使用密码创建一个文件:
(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))))
现在你可以做到:
(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))