How to run multiple lines of commands in Terminal

2019-09-17 10:25发布

Please refer to this question first,

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

Based on that I need to run the following three line Terminal commands using 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 .command and .sh extensions and added the above three lines. Then tried to run it from AppleScript as,

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 sudo related commands without asking for password in Terminal.

What code needs to be used to run from AppleScript?

2条回答
Ridiculous、
2楼-- · 2019-09-17 10:55

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
查看更多
混吃等死
3楼-- · 2019-09-17 11:07
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.

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.”

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
查看更多
登录 后发表回答