如何“以管理员权限执行shell脚本”作为普通用户而不是 root?
我正在运行一个 applscript,它通过终端运行 shellscript。
我希望能够使用“以管理员权限执行 shell 脚本”,但终端以 root 身份运行我的脚本,而不是以普通用户(即管理员)身份运行。
我不想以 root 身份运行的原因(除了明显的原因)是我使用 ~ 符号,以便任何用户都可以登录并在本地运行脚本(如果我将日志文件直接写入用户的桌面)。
我不想以 root 身份运行的另一个原因是,我在 shell 脚本中使用了 ViseX 安装程序,但该安装程序不能以 root 身份运行良好。
这是我使用的 applescript(感谢 regulus6633 的 applescirpt):
set theFile to "myscriptname.sh"
-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal > /dev/null 2>&1 & echo $!" with administrator privileges) as integer
-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
set theProcess to first process whose unix id is thePID
set bi to bundle identifier of theProcess
end tell
-- runs the Terminal with the shellscript
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
activate
open theFileAlias
end tell
I'm running an applscript that runs a shellscript through Terminal.
I want to be able to use "do shell script with administrator privileges" but the Terminal runs my script as root, and not as regular user (which is an Admin).
The reason I want to run not as root (except for the obvious reasons) is that I use the ~ sign so any user can log in and run the script locally (f.e. I write a log file straight to the user's Desktop).
The other reason I want to run not as root is because I use a ViseX installer during my shell script that does not run well as root.
This is the applescript I use (Thanks to regulus6633 for the applescirpt):
set theFile to "myscriptname.sh"
-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal > /dev/null 2>&1 & echo $!" with administrator privileges) as integer
-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
set theProcess to first process whose unix id is thePID
set bi to bundle identifier of theProcess
end tell
-- runs the Terminal with the shellscript
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
activate
open theFileAlias
end tell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了成功的方法:
这里发生了什么:
它有效(我现在在自动化应用程序中使用它)。
I have found approach to get success with next:
What is going here:
. ~/.bash_profile
environment variables (like a PATH).It works (I'm using it in automator application now).
不幸的是,
具有管理员权限
意味着“作为root”。在 Mac OS X 中,与大多数其他 Unix 派生操作系统一样,无法以非 root 用户身份获得管理员权限。当您通过 sudo 运行某些内容时,它只是以 root 身份运行该命令。不过,您不需要以 root 身份运行整个终端窗口。只需以普通用户身份启动终端并告诉它(通过
tell application "Terminal"
块内的do script
)以 root 身份执行所需的命令。我不知道do script
是否接受具有管理员权限
,但如果没有,您可以随时在前面加上sudo
。Unfortunately,
with administrator privileges
means "as root". In Mac OS X, as in most other Unix-derived operating systems, there's no way to get administrator privileges as a non-root user. When you run something viasudo
, it just runs the command as root.You don't need to run an entire Terminal window as root, though. Just launch Terminal as the regular user and tell it (via
do script
inside atell application "Terminal"
block) to execute just the desired command as root. I don't know offhand ifdo script
acceptswith administrator privileges
, but if not, you can always stick asudo
on the front.