bash 如何从 AppleScript 在终端中运行多行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24591690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:48:04  来源:igfitidea点击:

How to run multiple lines of commands in Terminal from AppleScript?

macosbashshellterminalapplescript

提问by Codename K

Please refer to this question first,

请先参考这个问题,

unable to read opensslv.h:No such file or directory

无法读取 opensslv.h: 没有那个文件或目录

Based on that I need to run the following three line Terminal commands using AppleScript,

基于此,我需要使用 AppleScript 运行以下三行终端命令,

/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared
make -f /tmp/ssl/openssl-1.0.1h/Makefile
sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install

I tried two methods I created text files with .commandand .shextensions and added the above three lines. Then tried to run it from AppleScript as,

我尝试了两种方法,我创建了扩展名为.command.sh 的文本文件,并添加了上述三行。然后尝试从 AppleScript 运行它,

do shell script "/Users/Username/Desktop/RunScript.sh"

But got this error,

但得到这个错误,

error "/Users/Username/Desktop/RunScript.sh: line 1: /tmp/ssl/openssl-1.0.1h/Configure: No such file or directory
/Users/Muhriz/Desktop/InstallOpenSSL.sh: line 2: make: command not found sudo: no tty present and no askpass program specified" number 1

This could work,

这可以工作,

tell application "Terminal" to activate
tell application "Terminal"
    do script ("/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared") in window 1
    do script ("make -f /tmp/ssl/openssl-1.0.1h/Makefile") in window 1
    do script ("sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install") in window 1
end tell

But it asks for password in Terminal at the third line and waits for user response. The password dialog shown from the AppleScript (when using with administrator privileges) is OK. But it must no ask for password via Terminal when running the commands. It needs to ask only once when the AppleScript is executed and run all sudorelated commands without asking for password in Terminal.

但是它在第三行的终端中要求输入密码并等待用户响应。AppleScript 中显示的密码对话框(使用 时with administrator privileges)是可以的。但是在运行命令时它不能通过终端询问密码。它只需要在 AppleScript 执行时询问一次并运行所有sudo相关命令,而无需在终端中询问密码。

What code needs to be used to run from AppleScript?

需要使用哪些代码才能从 AppleScript 运行?

回答by jww

Perform the following before running your scripts.

在运行脚本之前执行以下操作。

chmod a+x /Users/Username/Desktop/RunScript.sh
xattr -r -d "com.apple.quarantine" /tmp/ssl/openssl-1.0.1h

回答by Simon White

do shell script "/Users/Username/Desktop/RunScript.sh"

That doesn't work because you can't pass a path to the “do shell script” command, you can only pass it the contents of the actual script.

这是行不通的,因为您不能将路径传递给“do shell script”命令,您只能将实际脚本的内容传递给它。

If you want to run a bash script that is contained in its own file, you can use TextEdit to open the bash script file and set the contents of the file to a variable, which you can then pass to “do shell script.”

如果你想运行一个包含在它自己的文件中的 bash 脚本,你可以使用 TextEdit 打开 bash 脚本文件并将文件的内容设置为一个变量,然后你可以将它传递给“do shell script”。

tell application "TextEdit"
    set theDesktopPath to the path to the desktop folder as text
    set theShellScriptPath to theDesktopPath & "RunScript.sh"
    open file theShellScriptPath
    set theShellScript to the text of document 1
    set theScriptResult to do shell script theShellScript
    make new document
    set the text of document 1 to theScriptResult
end tell