bash – 具有输入和管理员权限的Applescript Execute Shell
我正在尝试编写一个automator服务来在终端中启动
virtualhost.sh.
使用“服务”上下文菜单,对话框将打开以询问虚拟主机的名称,然后运行applescript以启动终端并传入输入文本. 我想要的是将我的用户名和密码传递给管理员权限,这样我就不需要在终端中使用sudo传递它. 这可以用do shell脚本完成,但是执行bin / sh而virtualhost.sh是一个bash脚本,所以我得到错误bin / sh:virtualhost.sh命令找不到 或者我可以使用do script with command但这不允许我传入用户名和密码. 我的代码看起来像这样: on run {input,parameters} set vhost to "virtualhost.sh " & input tell application "Terminal" activate do shell script vhost user name "user" password "pass" with administrator privileges end tell end run 这会产生前面提到的bin / sh错误. 用do脚本命令 on run {input,parameters} set vhost to "virtualhost.sh " & input tell application "Terminal" activate do script with command vhost user name "user" password "pass" with administrator privileges end tell end run 这会产生一个转义错误:预期的行尾等,但找到了属性. 有没有办法正确地做到这一点? 解决方法
不熟悉AppleScript Studio,但如果您提供virtualhost.sh的完整路径,则可以使用普通的旧AppleScript(看起来具有相同的问题). (另外,“do shell script”不需要终端.)示例:
set vhost to "/usr/local/bin/virtualhost.sh " & input do shell script vhost user name "user" password "pass" ? with administrator privileges 您还可以扩展$PATH(默认为/usr/bin:/ bin:/usr/sbin:/ sbin with“do shell script”)以包含virtualhost.sh的路径,例如: set vhost to "{ PATH=$PATH:/usr/local/bin; virtualhost.sh " & input & "; }" do shell script vhost user name "user" password "pass" ? with administrator privileges 如果需要相对路径,可以将virtualhost.sh放在脚本应用程序或包中(例如在内容/资源中),在终端中或通过按住Control并单击并选择“显示包内容”.然后使用“path to me”: set vhostPath to "'" & POSIX path of (path to me) & ? "/Contents/Resources/virtualhost.sh" & "'" set vhost to vhostPath & space & input do shell script vhost user name "user" password "pass" ? with administrator privileges (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |