unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Control of fan-speed on Lenovo Thinkpads
Date: Tue, 30 Mar 2021 23:06:34 +0300	[thread overview]
Message-ID: <YGOEyil6gHj+y4bT@protected.localdomain> (raw)
In-Reply-To: <jwv35wcr8kl.fsf-monnier+emacs@gnu.org>

* Stefan Monnier <monnier@iro.umontreal.ca> [2021-03-30 18:03]:
> > (defun sudo (&rest arguments)
> >   "Executes list ARGUMENTS with system command `sudo'."
> >   (let ((default-directory "/sudo::"))
> >     (shell-command-to-string (string-join arguments " "))))
> 
> Please don't `string-join` arguments and please don't use a shell when
> it's not necessary: they're recipes for quoting bugs.
> Better use something like `file-process` here.

Yes, you are right, that it works well is coincidence. I think that
string-join came from earlier command which used call-process or
similar where arguments are separate.

I have improved my command, and it still works well. 

(defun sudo (command)
  "Execute COMMAND with system command `sudo'."
  (let ((not-remote (not (file-remote-p default-directory))))
    (if not-remote
	(let* ((command (format "sudo su -c -- root -c \"%s\"" command))
	       (return (shell-command-to-string command)))
	  return)
      (message "This `sudo' does not work on remote directory: %s" default-directory))))

You probably mean process-file? There is no file-process.

There is practical burden to use call-process, and it is with &rest
ARGS, as those have to be given all separately.

One echo to file and command with sudo becomes very complex, and it does not work:

(call-process "/usr/bin/sudo" nil (get-buffer-create "sudo") nil "su" "-c" "--" "root" "-c" "echo" "level" "full-speed" ">" "/proc/acpi/ibm/fan")

(call-process "/usr/bin/sudo" nil (get-buffer-create "sudo") nil "su" "-c" "--" "root" "-c" "echo" "hello" ">" "/tmp/text.txt") → 0 -- but does not produce /tmp/text.txt, so I do not know how to use it.

Then again I would like to pass a command, and not quote each part of command as arguments.

I would like something like (sudo "echo hello > /proc/file") that it works.

But for that to work, how I understand, I need to split string with spaces, remove empty strings maybe, and then pass it as list as ARGS to call-process.

(setq args '("su" "-c" "--" "root" "-c" "echo" "level" "full-speed" ">" "/proc/acpi/ibm/fan"))

(call-process "sudo" nil t t args) -> this will not work, so to
pass it as a list there, I would need to make a macro expansion?

`(call-process "sudo" nil (get-buffer-create "sudo") nil ,@args) → (call-process "sudo" nil (get-buffer-create "sudo") nil "su" "-c" "--" "root" "-c" "echo" "level" "full-speed" ">" "/proc/acpi/ibm/fan")

(defun sudo (command)
  "Execute COMMAND with system command `sudo'."
  (let ((not-remote (not (file-remote-p default-directory))))
    (if not-remote
	(let* ((sudo "sudo")
	       (args (append '("su" "-c" "--" "root" "-c") (split-string command)))
	       (status `(call-process ,sudo nil (get-buffer-create "*sudo*") t ,@args)))
	  (message "%s" status)
	  (eval status)))))

By using that above function, this will work: (sudo "ls") but this will not work:
(sudo "ls /boot /var") -- so I do not know how to expand the list properly in the function. 

I would like to get following to work in simpler way by providing string:

(sudo "echo level full-speed > /proc/acpi/ibm/fan")

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns



  reply	other threads:[~2021-03-30 20:06 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-29 20:36 Control of fan-speed on Lenovo Thinkpads Jean Louis
2021-03-30  8:12 ` Michael Albinus
2021-03-30  9:42   ` Jean Louis
2021-03-30  9:44   ` Jean Louis
2021-03-30 10:13     ` Michael Albinus
2021-03-30 10:23       ` Finding simpler better sudo for Emacs Jean Louis
2021-03-30 10:34         ` Michael Albinus
2021-03-30 10:43           ` Jean Louis
2021-03-30 10:52             ` Michael Albinus
2021-03-30 11:05               ` Jean Louis
2021-03-30 11:13                 ` Michael Albinus
2021-03-30 11:40                   ` Jean Louis
2021-03-30 15:01   ` Control of fan-speed on Lenovo Thinkpads Stefan Monnier
2021-03-30 20:06     ` Jean Louis [this message]
2021-03-31  1:23       ` Stefan Monnier
2021-03-31  5:35         ` Jean Louis
2021-03-31 14:23           ` Stefan Monnier
2021-03-31 20:02             ` Jean Louis
2021-03-31 20:19               ` Stefan Monnier
2021-04-01  9:46                 ` Jean Louis
     [not found] <8735wcogti.fsf@gmail.com>
     [not found] ` <YGNq8IGh12I+QL9I@protected.localdomain>
2021-03-31  5:49   ` Utkarsh Singh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YGOEyil6gHj+y4bT@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).