From 3e5d3044d7dba550536b0560ce150eebbaf7ecfc Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Sat, 12 Oct 2024 20:48:07 +0200 Subject: [PATCH] Terminal: Simplify shell selection Largely based on @benoit-pierre's original patch in https://github.com/koreader/koreader/pull/12384#issuecomment-2298130062 --- plugins/terminal.koplugin/main.lua | 31 ++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/plugins/terminal.koplugin/main.lua b/plugins/terminal.koplugin/main.lua index 6e05e2a3a..ec9647258 100644 --- a/plugins/terminal.koplugin/main.lua +++ b/plugins/terminal.koplugin/main.lua @@ -93,28 +93,35 @@ local Terminal = WidgetContainer:extend{ } function Terminal:isExecutable(file) - if os.execute(string.format("test -x %s", file)) == 0 then -- full path - return true - elseif os.execute(string.format("which %s 2>/dev/null 1>/dev/null", file)) == 0 then - return true - end + -- check if file is an executable or a command in PATH + return os.execute(string.format("test -x %s || command -v %s", file, file)) == 0 end -- Try SHELL environment variable and some standard shells function Terminal:getDefaultShellExecutable() if self.default_shell_executable then return self.default_shell_executable end - local shell = {"mksh", "ksh", "zsh", "ash", "dash", "sh", "bash"} - table.insert(shell, os.getenv("SHELL")) + local shell = { + "bash", + "sh", + "dash", + "ash", + "zsh", + "ksh", + "mksh", + } + local env_shell = os.getenv("SHELL") + if env_shell then + table.insert(shell, 1, env_shell) + end - while #shell >= 1 do - if self:isExecutable(shell[#shell]) then - self.default_shell_executable = shell[#shell] + for dummy, file in ipairs(shell) do + if self:isExecutable(file) then + self.default_shell_executable = file break - else - shell[#shell] = nil end end + logger.info("Terminal: default shell is", self.default_shell_executable) return self.default_shell_executable end