更新
“我接受了”肖恩的回答,只是做了一些小小的修改。
(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正确安装gems。我如何用sudo在elisp中调用这个shell命令,输入密码,并且仍然能够在生成的窗口中显示结果?
发布于 2014-04-09 06:12:09
下面是一个助手函数,它使用提供的密码作为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)))发布于 2014-04-07 16:30:05
您需要用密码创建一个文件:
(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))https://stackoverflow.com/questions/22903910
复制相似问题