From 1392793334c14d1cb3f4b95eb191a0685e886143 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Tue, 8 Nov 2022 18:52:39 +0100 Subject: [PATCH] sftp: auto-detect shell type for fish Fish is different from POSIX-based Unix shells such as bash, and a bracketed variable references like we use for the auto-detection echo command is not supported. The command will return with zero exit code but produce no output on stdout. There is a message on stderr, but we don't log it due to the zero exit code: fish: Variables cannot be bracketed. In fish, please use {$ShellId}. Fixes #6552 --- backend/sftp/sftp.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index b6afcd355..109745267 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -975,20 +975,24 @@ func NewFsWithConnection(ctx context.Context, f *Fs, name string, root string, m fs.Debugf(f, "Running shell type detection remote command: %s", shellCmd) err = session.Run(shellCmd) _ = session.Close() + f.shellType = defaultShellType if err != nil { - f.shellType = defaultShellType fs.Debugf(f, "Remote command failed: %v (stdout=%v) (stderr=%v)", err, bytes.TrimSpace(stdout.Bytes()), bytes.TrimSpace(stderr.Bytes())) } else { outBytes := stdout.Bytes() fs.Debugf(f, "Remote command result: %s", outBytes) outString := string(bytes.TrimSpace(stdout.Bytes())) - if strings.HasPrefix(outString, "Microsoft.PowerShell") { // If PowerShell: "Microsoft.PowerShell%ComSpec%" - f.shellType = "powershell" - } else if !strings.HasSuffix(outString, "%ComSpec%") { // If Command Prompt: "${ShellId}C:\WINDOWS\system32\cmd.exe" - f.shellType = "cmd" - } else { // If Unix: "%ComSpec%" - f.shellType = "unix" - } + if outString != "" { + if strings.HasPrefix(outString, "Microsoft.PowerShell") { // PowerShell: "Microsoft.PowerShell%ComSpec%" + f.shellType = "powershell" + } else if !strings.HasSuffix(outString, "%ComSpec%") { // Command Prompt: "${ShellId}C:\WINDOWS\system32\cmd.exe" + // Additional positive test, to avoid misdetection on unpredicted Unix shell variants + s := strings.ToLower(outString) + if strings.Contains(s, ".exe") || strings.Contains(s, ".com") { + f.shellType = "cmd" + } + } // POSIX-based Unix shell: "%ComSpec%" + } // fish Unix shell: "" } } // Save permanently in config to avoid the extra work next time