简体   繁体   English

使用 go 获取 shell 名称

[英]Get shell name with go

I'm running an CLI app and I want to know the user's shell.我正在运行一个 CLI 应用程序,我想知道用户的 shell。 I tried to use我试着用

exec.Command("echo", "$0")

but it logs $0 .但它记录$0 Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

You probably want two things:你可能想要两件事:

  1. the UID of the current process当前进程的 UID

  2. the configured shell of that UID该 UID 的配置外壳

os/user can give you the former but after quick searches for 'passwd' or 'pwent' or 'getpwent' (the appropriate libc call) I haven't found a go library that can get you the latter. os/user可以给你前者,但在快速搜索 'passwd' 或 'pwent' 或 'getpwent'(适当的 libc 调用)后,我还没有找到一个可以让你获得后者的 go 库。 /etc/passwd is trivial to parse and includes this information once you have the UID, however, so this is what I'd do. /etc/passwd解析起来很简单,一旦你有了 UID,它就会包含这些信息,所以这就是我要做的。

Windows is obviously going to be different, but Darwin might also actually ignore this /etc file and require that you call libc to reach the truth. Windows 显然会有所不同,但 Darwin 实际上也可能会忽略此 /etc 文件,并要求您调用 libc 以了解真相。

It's also conceiveable that your program is always called with some other shell than the configured one, and if this is your concern you'll have to use something like go-ps and trace PPIDs until you find something that you believe to be a shell.这也是conceiveable你的程序总是被调用一些其他的shell大于配置之一,如果这是你的关心,你必须使用像去-PS和跟踪PPIDs,直到你找到的东西,你认为是一个外壳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM