自制软件如何防止与 sudo 一起使用?

How does homebrew prevent being used with sudo?

当我尝试使用具有 sudo 权限的 homebrew 安装时,例如 sudo brew install lua,我收到此消息

Error: Running Homebrew as root is extremely dangerous and no longer supported. As Homebrew does not drop privileges on installation you would be giving all build scripts full access to your system.

我理解这个错误并认为这是个好主意,但是 brew 如何检测到我正在使用 sudo?我也可以在我的应用程序中实现此功能吗?

brew.sh 使用 id -u 验证它是否以 root 身份运行。如果它以 root 身份运行,它将 return 0。请参阅此 line。之后就是根据获取到的id显示消息的问题了。