我有一个shell脚本,在OS X上有用户执行权限,但当我双击它时,它在文本编辑器中打开。如何通过双击它来运行它?
First in terminal make the script executable by typing the following command: 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.
无需使用Platypus等第三方应用。
只需使用脚本编辑器创建一个Apple脚本,并使用命令do shell Script“shell commands”进行直接命令调用或可执行的shell脚本文件,将可编辑的脚本文件安全保存在某个地方,然后导出它以创建应用程序脚本。应用程序脚本可以通过双击或选择栏文件夹启动。
从OSX 10.10 (Yosemite)和至少OSX 10.8 (Mountain Lion)开始,当你从Finder打开(双击)可执行脚本时,行为如下:
Executable scripts[1] with either NO suffix or suffix .command: are executed by default - no setup required: a new Terminal window opens in which the script runs. by default, the window will remain open after the script terminates so you can inspect the output (though at that point the shell that ran the script has exited and you cannot interact with it any longer). However, via Terminal's Preferences... > Profiles you can opt to automatically close the window when the script exits. Caveat: the working folder is 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 symlinked script's true source directory, see this answer. If the script is unexpectedly not executable: 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 via File > Get Info, which is not true - use the chmod +x method suggested above. no suffix: with a shebang line (e.g., #!/bin/bash): behavior is as if the suffix were .sh - see below. with no shebang line: opens in your default text editor (which is TextEdit by default). Scripts with suffix .sh, whether executable or not: are opened for editing in TextEdit.app or, if installed, with Xcode.app. Scripts with suffix .scpt or .applescript (even if they're themselves marked as executable, which is not normally the case): opened for editing in [Apple]Script Editor Note that the JXA source-code files seem to have no distinct suffix (yet). Scripts with a custom suffix (a suffix not yet known to the system), whether executable or not (in fact, applies to any kind of file): prompt you for the app to open them with when you first open them, and remember that choice.
[1]可执行的意思是:具有可执行权限位和调用用户(相对于文件的所有权)的脚本,因此可能被允许执行它。 如果使用chmod a+x来设置所有权限位(这是典型的),任何人都可以调用它(假设他们也可以根据读权限位和文件的所有权来读取文件)。
简单的方法是将扩展名更改为.command或不扩展名。
但这会打开终点站,你必须关闭终点站。如果你不想看到任何输出,你可以使用Automator创建一个Mac应用程序,你可以双击,添加到dock等。
打开Automator应用程序 选择“应用程序”类型 在Actions搜索框中输入“run” 双击“运行Shell脚本” 单击右上角的Run按钮来测试它。 文件>保存以创建应用程序。
你也可以使用RCDefaultApp通过文件扩展名设置默认值:
http://www.rubicode.com/Software/RCDefaultApp/
潜在地,你可以设置.sh打开在iTerm/终端等,这需要用户执行权限,例如
chmod u+x filename.sh
推荐文章
- 在Bash中将命令行参数转换为数组
- 为什么用反斜杠开始shell命令?
- 遍历带空格的文件列表
- 警告用户/local/mysql/data目录不属于mysql用户
- 在Bash中检查传递的参数是否为文件或目录
- 递归复制文件夹,不包括一些文件夹
- 如何使Python脚本在Linux中像服务或守护进程一样运行
- 如何在文件中grep不区分大小写的字符串?
- 如何使用文件的行作为命令的参数?
- 如何从命令输出中获得第二列?
- 在Mac OS X上使用鼠标聚焦(加上自动提升)
- 寻找ALT+LeftArrowKey解决方案在zsh
- 我在哪里可以找到Mac OS X Lion的“make”程序?
- 如何做一个非贪婪匹配在grep?
- 当我没有Mac的时候,在Mac/Safari上测试web应用程序