From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Control of fan-speed on Lenovo Thinkpads Date: Tue, 30 Mar 2021 23:06:34 +0300 Message-ID: References: <87blb13vr8.fsf@gmx.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4330"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0.6 (2021-03-06) Cc: help-gnu-emacs@gnu.org To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Mar 30 22:12:19 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRKip-00010F-6E for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 30 Mar 2021 22:12:19 +0200 Original-Received: from localhost ([::1]:36568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lRKio-0003cv-6V for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 30 Mar 2021 16:12:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRKi9-0003ck-S4 for help-gnu-emacs@gnu.org; Tue, 30 Mar 2021 16:11:37 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:51623) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lRKi7-0005Oq-TD for help-gnu-emacs@gnu.org; Tue, 30 Mar 2021 16:11:37 -0400 Original-Received: from localhost ([::ffff:41.202.241.58]) (AUTH: PLAIN securesender, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000027F39.00000000606385F4.00002E3F; Tue, 30 Mar 2021 13:11:31 -0700 Mail-Followup-To: Stefan Monnier , help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:128768 Archived-At: * Stefan Monnier [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