简体   繁体   English

如何在 Mac 上运行 shell 命令?

[英]How to run shell command on Mac?

How could I run a shell command using Excel VBA code?我如何使用 Excel VBA 代码运行 shell 命令?

I want to run chmod +x /Users/asdasd/Music/playlistLoad.sh through the shell.我想通过 shell 运行chmod +x /Users/asdasd/Music/playlistLoad.sh /Users/asdasd/Music/playlistLoad.sh。

I achieved this using Python and os.system("Command here") .我使用 Python 和os.system("Command here")实现了这一点。

The Shell command appears to be broken in the later versions of Excel on Mac. Shell 命令在 Mac 上的 Excel 的更高版本中似乎已损坏。 I couldn't get Shell to work at all on Excel v15.58 and I believe its due to sandbox issues.我无法让 Shell 在 Excel v15.58 上工作,我相信这是由于沙箱问题。

The solution I found was to create a wrapper function in AppleScript that can be called from VBA and then executes the shell command and returns the result.我找到的解决方案是在 AppleScript 中创建一个包装器 function,可以从 VBA 调用它,然后执行 shell 命令并返回结果。

The extra hurdle (on top of creating the AppleScript) is that this script must be located in:额外的障碍(在创建 AppleScript 之上)是该脚本必须位于:

~/Library/Application Scripts/com.microsoft.Excel ~/Library/Application Scripts/com.microsoft.Excel

Here is my wrapper script (saved as ShellExScript.scpt):这是我的包装器脚本(另存为 ShellExScript.scpt):

on ShellEx(shellCmd)
    do shell script shellCmd
end ShellEx

You can then execute shell commands from within excel like so:然后,您可以从 excel 中执行 shell 命令,如下所示:

Dim commandResult As String
Dim commandToRun As String

commandToRun = "ls ~/"

commandResult = AppleScriptTask("ShellExScript.scpt","ShellEx",commandToRun)

Range("A1").Value = commandResult

You will need to call the shell function. 您将需要调用shell函数。 So your code would be 所以你的代码是

Shell("chmod +x /Users/asdasd/Music/playlistLoad.sh")

The shell function will work on any OS, however you need to adapt the command you pass to it to the OS you are working on. Shell函数可以在任何操作系统上使用,但是您需要将传递给它的命令调整为适合您正在使用的操作系统。 In your exemple, you have the UNIX style, so it will work only on Linux/MacOs or other *NIX systems. 例如,您具有UNIX风格,因此它仅适用于Linux / MacO或其他* NIX系统。 Windows don't have the notion of executable in it's permissions so there is no equivalent for your command in Windows anyway. Windows的权限中没有可执行文件的概念,因此Windows中的命令始终没有等效项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM