macos 如何通过双击在 OS X 中运行 shell 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5125907/
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
How to run a shell script in OS X by double-clicking?
提问by c2h2
I have a shell script that has user execution permission on OS X, but when I double click on it, it opens in a text editor. How can I get it to run by double-clicking it?
我有一个在 OS X 上具有用户执行权限的 shell 脚本,但是当我双击它时,它会在文本编辑器中打开。我怎样才能通过双击它来运行它?
回答by Lus
First in terminal make the script executable by typing:
chmod a+x (yourscriptname)
Then, in Finder, right-click your file and select "Open with" and then "Other...".
Here you select the application you want the file to execute into, in this case it would be Terminal. To be able to select terminal you need to switch from "Recommended Applications" to "All Applications". (The Terminal.app application can be found in the Utilities folder)
NOTE that unless you don't want to associate all files with this extension to be run in terminal you should not have "Always Open With" checked.
- After clicking OK you should be able to execute you script by simply double-clicking it.
首先在终端中输入以下命令使脚本可执行:
chmod a+x (yourscriptname)
然后,在 Finder 中,右键单击您的文件并选择“打开方式”,然后选择“其他...”。
在这里,您选择要执行文件的应用程序,在本例中为终端。为了能够选择终端,您需要从“推荐应用程序”切换到“所有应用程序”。(Terminal.app 应用程序可以在 Utilities 文件夹中找到)
请注意,除非您不想将所有文件与此扩展名关联以在终端中运行,否则您不应选中“始终打开方式”。
- 单击“确定”后,您应该可以通过简单地双击脚本来执行您的脚本。
回答by Hagelin
Have you tried using the .command
filename extension?
您是否尝试过使用.command
文件扩展名?
回答by mklement0
As of OSX 10.10(Yosemite) and since at leastOS X 10.8(Mountain Lion), the behavior is as follows when you open (double-click) executable scriptsfrom Finder:
从 OSX 10.10(Yosemite) 和至少OS X 10.8(Mountain Lion) 开始,当您从 Finder打开(双击)可执行脚本时,行为如下:
- Executablescripts[1]with either NO suffixor suffix
.command
:- are executedby default - no setup required:
- a newTerminal window opens in which the script runs.
- by default, the window will remain openafter the script terminates so you can inspect the output(though at that point the shell that ran the script has exited and you cannot interactwith it any longer).
However, via Terminal'sPreferences... > Profiles
you can opt to automatically closethe window when the script exits.
- Caveat: the working folderis invariably the current user's home folder, NOT the folder in which the script is located.
- To make a shell script change to the folder in which it is located, place
cd -- "$(dirname "$BASH_SOURCE")"
right after the shebang line- or, if you must remain POSIX-compliant,
cd -- "$(dirname "$0")"
. - For edge cases, such as finding a symlinkedscript's true source directory, see this answerof mine.
- To make a shell script change to the folder in which it is located, place
- If the script is unexpectedly notexecutable:
- Make it executable by running
chmod +x <script>
in Terminal; otherwise, you'll see the following symptoms: .command
: Finder displays a misleading error message that suggests the problem can be fixed viaFile > Get Info
, which is not true - use thechmod +x
method suggested above.- no suffix:
- witha shebang line (e.g.,
#!/bin/bash
): behavior is as if the suffix were.sh
- see below. - with noshebang line: opens in your default text editor (which is TextEdit by default).
- witha shebang line (e.g.,
- Make it executable by running
- are executedby default - no setup required:
- Scripts with suffix
.sh
, whether executable or not:- are opened for editingin
TextEdit.app
or, if installed, withXcode.app
.
- are opened for editingin
- Scripts with suffix
.scpt
or.applescript
(even if they're themselves marked as executable, which is not normally the case):- opened for editingin
[Apple]Script Editor
- Note that the JXAsource-code files seem to have no distinct suffix (yet).
- opened for editingin
- Scripts with a customsuffix(a suffix not yet known to the system), whether executable or not (in fact, applies to any kind of file):
- promptyoufor the app to open them with when you firstopen them, and remember that choice.
- 可执行的脚本[1]与任一NO后缀或后缀
.command
:- 正在执行默认-不需设置:
- 一个新的终端窗口打开,脚本在其中运行。
- 默认情况下,窗口仍将打开脚本终止后,这样你就可以检查输出(虽然在这一点上运行该脚本退出外壳和你不能交互使用任何更长)。
但是,通过终端,Preferences... > Profiles
您可以选择在脚本退出时自动关闭窗口。
- 警告:工作文件夹总是当前用户的主文件夹,而不是脚本所在的文件夹。
- 要将 shell 脚本更改为它所在的文件夹,请将
cd -- "$(dirname "$BASH_SOURCE")"
就在shebang线之后- 或者,如果您必须保持 POSIX 兼容,
cd -- "$(dirname "$0")"
. - 对于边缘情况,例如查找符号链接脚本的真实源目录,请参阅我的这个答案。
- 要将 shell 脚本更改为它所在的文件夹,请将
- 如果脚本意外不可执行:
- 通过
chmod +x <script>
在终端中运行使其可执行;否则,您会看到以下症状: .command
:Finder 显示一条误导性的错误消息,表明问题可以通过 修复File > Get Info
,但事实并非如此 - 使用chmod +x
上面建议的方法。- 无后缀:
- 带有shebang行(例如,
#!/bin/bash
):行为就像后缀一样.sh
- 见下文。 - 有没有家当线:在默认的文本编辑器中打开(这是文字编辑默认情况下)。
- 带有shebang行(例如,
- 通过
- 正在执行默认-不需设置:
- 带有后缀的
.sh
脚本,无论是否可执行:- 正在打开编辑在
TextEdit.app
或者,如果已安装,使用Xcode.app
。
- 正在打开编辑在
- 带有后缀
.scpt
or 的脚本.applescript
(即使它们本身被标记为可执行文件,通常情况并非如此):- 打开编辑在
[Apple]Script Editor
- 请注意,JXA源代码文件似乎没有明显的后缀(目前)。
- 打开编辑在
- 与脚本一个自定义的后缀(后缀还不知道系统),无论是可执行文件或没有(其实适用于任何类型的文件):
- 首次打开它们时会提示您使用应用程序打开它们,并记住该选择。
[1] Executablemeans: a script with the executable permission bit(s) set and the calling user - relative to the ownership to the file - therefore potentially being allowed to execute it.
If you use chmod a+x
to set allpermission bits (which is typical), anyonecan invoke it (assuming they're also allowed to readthe file based on the readpermission bit(s) and the file's ownership).
[1]可执行的意思是:一个设置了可执行权限位和调用用户的脚本——相对于文件的所有权——因此可能被允许执行它。
如果使用chmod a+x
设置的所有权限位(这是典型的),任何人都可以调用它(假设他们也被允许阅读基础上的文件读取权限位(S)和文件的所有权)。
回答by svth
回答by wisbucky
The easy way is to change the extension to .command
or no extension.
简单的方法是将扩展名更改为.command
或不使用扩展名。
But that will open the Terminal, and you will have to close it. If you don't want to see any output, you can use Automator to create a Mac Application that you can double click, add to the dock, etc.
但这将打开终端,您将不得不关闭它。如果你不想看到任何输出,你可以使用 Automator 创建一个 Mac 应用程序,你可以双击它,添加到 Dock 等。
- Open
Automator
application - Choose "Application" type
- Type "run" in the Actions search box
- Double click "Run Shell Script"
- Click the
Run
button in upper right corner to test it. File > Save
to create the Application.
- 打开
Automator
申请 - 选择“申请”类型
- 在操作搜索框中键入“运行”
- 双击“运行Shell脚本”
- 单击右上角的
Run
按钮进行测试。 File > Save
创建应用程序。
回答by Luc-Olivier
No need to use third-party apps such as Platypus.
无需使用 Platypus 等第三方应用程序。
Just create an Apple Script with Script Editor and use the command do shell script "shell commands"
for direct command calls or executable shell script files, keep the editable script file safe somewhere then export it to create an Application script. the app script is launch-able by double click or selection in bar folder.
只需使用脚本编辑器创建 Apple 脚本并使用该命令do shell script "shell commands"
进行直接命令调用或可执行的 shell 脚本文件,将可编辑的脚本文件保存在安全的某处,然后将其导出以创建应用程序脚本。应用程序脚本可以通过双击或在栏文件夹中选择来启动。
回答by Tomachi
You can also set defaults by file extension using RCDefaultApp:
您还可以使用 RCDefaultApp 按文件扩展名设置默认值:
http://www.rubicode.com/Software/RCDefaultApp/
http://www.rubicode.com/Software/RCDefaultApp/
potentially you could set .sh to open in iTerm/Terminal etc. it would need user execute permissions, eg
可能您可以将 .sh 设置为在 iTerm/Terminal 等中打开。它需要用户执行权限,例如
chmod u+x filename.sh
回答by Neo
chmod 774 filename
chmod 774 filename
Note: The file with name 'filename' that has the bash script has no extension
注意:具有 bash 脚本的名为“filename”的文件没有扩展名