From 4a36cc0266f196e55fb277e4cac2f4fdc478806a Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 19 Nov 2026 18:16:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(device):=20shell=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=BB=9D=E5=AF=B9=E8=B7=AF=E5=BE=84=E4=B8=8E?= =?UTF-8?q?=20busybox=EF=BC=8C=E9=BB=98=E8=AE=A4=E4=BB=8D=E4=B8=BA=20sh/cm?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不做自动探测(各系统 shell 变体太多,交给配置);配了路径就按 POSIX 语义 -c 执行, busybox 走 sh -c;上报给 core 的 shell 归成短标签(bash/busybox/sh/cmd/powershell)。 --- device/README.md | 14 ++++++++++++ device/cmd/agent/main.go | 48 +++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/device/README.md b/device/README.md index 594d2cd..e68e93a 100644 --- a/device/README.md +++ b/device/README.md @@ -63,6 +63,20 @@ sh scripts/build.sh all 优先级:**命令行显式参数 > 环境变量 `NODE_TOKEN`(仅 token)> 配置文件 > 默认值**。 +### shell 用哪个 + +默认:macOS / Linux 用 `/bin/sh`(固定,不读 `$SHELL`——用户把默认 shell 设成 fish 时, +模型写的 POSIX 语法会全废);Windows 用 `cmd`。 + +想换就在 `config.json` 里写 `shell`,**不做自动探测**:装了 Git Bash / busybox 就写路径, +没装就保持默认。 + +```json +{ "shell": "C:\\Program Files\\Git\\bin\\bash.exe" } +``` + +报给 core 的是短标签(`sh` / `bash` / `busybox` / `cmd` / `powershell`),同时带上解释器路径与版本。 + | 参数 | 说明 | |---|---| | `-server` | core 的接入地址,`wss://…/dev/ws` | diff --git a/device/cmd/agent/main.go b/device/cmd/agent/main.go index 6441021..870d841 100644 --- a/device/cmd/agent/main.go +++ b/device/cmd/agent/main.go @@ -171,7 +171,7 @@ func main() { host = *name } - log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, r.shell, *server) + log.Printf("agent-device %s 启动:root=%s shell=%s → %s", version, r.root, shellKind(r.shell), *server) shPath, shVer := shellInfo(r.shell) log.Printf("shell:%s(%s %s)", r.shell, shPath, shVer) cfg := wsnode.Config{URL: *server, Token: *token, Insecure: *insecure, FixClock: *fixClock} @@ -181,7 +181,7 @@ func main() { if err := c.Send(proto.Msg{ T: proto.TKindHello, Ver: version, Name: host, OS: runtime.GOOS, Arch: runtime.GOARCH, - Shell: r.shell, ShellPath: shPath, ShellVer: shVer, Root: r.root, + Shell: shellKind(r.shell), ShellPath: shPath, ShellVer: shVer, Root: r.root, }); err != nil { return err } @@ -361,6 +361,9 @@ func flatten(env map[string]string) []string { return out } +// defaultShell 选默认解释器。POSIX 上固定 /bin/sh(不读 $SHELL:用户把 shell 设成 fish +// 之后模型写的 POSIX 语法全废);Windows 默认 cmd,想用 git-bash / busybox 就在 +// config.json 里写 "shell": "C:\\Program Files\\Git\\bin\\bash.exe"(或 PATH 里的名字)。 func defaultShell() string { if runtime.GOOS == "windows" { return "cmd" @@ -368,21 +371,46 @@ func defaultShell() string { return "sh" } +// shellKind 把路径/命令名归成一个短标签给 core 看(hello 的 shell 字段)。 +func shellKind(p string) string { + b := strings.ToLower(filepath.Base(p)) + switch { + case strings.Contains(b, "busybox"): + return "busybox" + case strings.Contains(b, "bash"): + return "bash" + case b == "sh" || b == "sh.exe" || b == "/bin/sh": + return "sh" + case strings.Contains(b, "powershell"), b == "pwsh", b == "pwsh.exe": + return "powershell" + case b == "cmd" || b == "cmd.exe": + return "cmd" + } + return p +} + func shellCmd(name string) (string, []string) { switch name { case "cmd": return "cmd", []string{"/c"} case "powershell", "pwsh": return name, []string{"-NoProfile", "-NonInteractive", "-Command"} - default: - if runtime.GOOS == "windows" { - return "cmd", []string{"/c"} - } - if _, err := os.Stat("/bin/sh"); err == nil { - return "/bin/sh", []string{"-c"} - } - return "sh", []string{"-c"} } + // busybox 的子命令形式:busybox sh -c "…" + if strings.Contains(strings.ToLower(filepath.Base(name)), "busybox") { + return name, []string{"sh", "-c"} + } + // 带路径的(git-bash 绝对路径、/bin/sh)直接当 POSIX shell 用 + if strings.ContainsAny(name, `\/`) { + return name, []string{"-c"} + } + if runtime.GOOS == "windows" { + return "cmd", []string{"/c"} + } + if _, err := os.Stat("/bin/sh"); err == nil { + return "/bin/sh", []string{"-c"} + } + return "sh", []string{"-c"} } // shellInfo 探测实际解释器与版本。这是模型写命令的唯一入口,所以路径要解开符号链接