单击时运行可执行的 bash 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3538592/
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
Making an executable bash file run when clicked
提问by jamesatha
I have a bash file that does some file manipulation. I don't want to have to open the terminal every time I run it. Is there a way to make the program run when I double click it? (Like a windows .exe file)
我有一个 bash 文件,可以进行一些文件操作。我不想每次运行时都打开终端。当我双击它时,有没有办法让程序运行?(就像一个 windows .exe 文件)
Thanks
谢谢
回答by Gordon Davisson
You can add a ".command" extension to the filename -- then double-clicking it will automatically open Terminal and run the script in a new window. Note: this assumes you still want to watch/interact with the script via a Terminal interface; if you want to avoid this as well, wrapping the script with Platypus, AppleScript, or Automator (as Zifei and Ned suggest) would be better options.
您可以为文件名添加“.command”扩展名——然后双击它会自动打开终端并在新窗口中运行脚本。注意:这假设您仍然想通过终端界面观看/与脚本交互;如果您也想避免这种情况,使用 Platypus、AppleScript 或 Automator(如 Zifei 和 Ned 建议的)包装脚本将是更好的选择。
回答by Zifei Tong
What you need is Platypus
.
你需要的是 Platypus
。
Platypus is a developer tool for the Mac OS X operating system. It can be used to create native, flawlessly integrated Mac OS X applications from interpreted scripts such as shell scripts or Perl and Python programs. This is done by wrapping the script in an application bundle directory structure along with an executable binary that runs the script.
Platypus 是 Mac OS X 操作系统的开发工具。它可用于从解释性脚本(如 shell 脚本或 Perl 和 Python 程序)创建原生的、完美集成的 Mac OS X 应用程序。这是通过将脚本与运行脚本的可执行二进制文件一起包装在应用程序包目录结构中来完成的。
回答by Nina
The easiest thing to do is to type: sudo chmod 755 the_file_Name
.This will allow you to double click on the file in the finder.
最简单的方法是输入:sudo chmod 755 the_file_Name
。这将允许您双击查找器中的文件。
回答by Ned Deily
With OS X 10.5+, you can wrap the bash shell script in an AppleScript
applicationusing the AppleScript
editor or an Automator
application using Automator.app
(see the Automator
on-line help).
使用 OS X 10.5+,您可以使用编辑器或应用程序将 bash shell 脚本包装在AppleScript
应用程序中(请参阅在线帮助)。AppleScript
Automator
Automator.app
Automator
You could write (and there are apps out there that do this) an OS X app that accepts arbitrary .sh
files and executes them. However, that's generally a bad idea as it could open you up to attacks if you inadvertently download a malicious shell script file that is automatically opened by your web browser. Better to be explicit.
您可以编写(并且有一些应用程序可以执行此操作)一个接受任意.sh
文件并执行它们的 OS X 应用程序。但是,这通常是一个坏主意,因为如果您无意中下载了由您的 Web 浏览器自动打开的恶意 shell 脚本文件,它可能会让您受到攻击。最好是明确的。