我有一个shell脚本,在OS X上有用户执行权限,但当我双击它时,它在文本编辑器中打开。如何通过双击它来运行它?


当前回答

你也可以使用RCDefaultApp通过文件扩展名设置默认值:

http://www.rubicode.com/Software/RCDefaultApp/

潜在地,你可以设置.sh打开在iTerm/终端等,这需要用户执行权限,例如

chmod u+x filename.sh

其他回答

Chmod 774文件名

注意:带有bash脚本的文件名为'filename'的文件没有扩展名

你也可以使用RCDefaultApp通过文件扩展名设置默认值:

http://www.rubicode.com/Software/RCDefaultApp/

潜在地,你可以设置.sh打开在iTerm/终端等,这需要用户执行权限,例如

chmod u+x filename.sh

或者,你也可以使用Platypus从你的脚本创建一个常规的Mac OS X应用程序

你试过使用.command文件扩展名吗?

从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来设置所有权限位(这是典型的),任何人都可以调用它(假设他们也可以根据读权限位和文件的所有权来读取文件)。