bash 如何通过双击使这个 file.sh 可执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8409946/
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 do I make this file.sh executable via double click?
提问by Jacob
First off I'm using Mac.
首先,我使用的是 Mac。
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
接下来,我需要执行这个“file.sh”,我们将调用它。每次我需要执行它时,我都必须打开终端并输入:
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?
这没关系,但我觉得如果我让文件双击执行会快很多,你不觉得吗?
So my question is, how do I make this file executable via double click?
所以我的问题是,如何通过双击使该文件可执行?
My ideas were either:
我的想法是:
a) type something like chmod into terminal and change permissions?
a) 在终端中输入类似 chmod 的内容并更改权限?
b) make a file, put code I wrote above in it ^ and then make that file executable?
b) 创建一个文件,将我上面写的代码放入其中 ^ 然后使该文件可执行?
c) make an automation somehow to do this?
c)以某种方式进行自动化来做到这一点?
Which way is best, or is there an even better way? Also please explain as much as you can, I'm new to Terminal. Thanks.
哪种方式最好,或者有更好的方式?另外请尽可能多地解释,我是终端的新手。谢谢。
回答by Chris Page
By default, *.sh
files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command
. By default, these are sent to Terminal, which will execute the file as a shell script.
默认情况下,*.sh
文件在文本编辑器(Xcode 或 TextEdit)中打开。要创建一个在终端打开时将在终端中执行的 shell 脚本,请使用“command”扩展名命名它,例如file.command
. 默认情况下,这些被发送到终端,终端将把文件作为 shell 脚本执行。
You will also need to ensure the file is executable, e.g.:
您还需要确保该文件是可执行的,例如:
chmod +x file.command
Without this, Terminal will refuse to execute it.
没有这个,终端将拒绝执行它。
Note that the script does not have to begin with a #!
prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #!
line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)
请注意,#!
在此特定场景中,脚本不必以前缀开头,因为终端专门安排使用您的默认 shell 执行它。(当然,#!
如果您想自定义使用哪个 shell,或者如果您想确保在使用不同的 shell 时可以从命令行执行它,您可以添加一行。)
Also note that Terminal executes the shell script without changing the working directory. You'll need to begin your script with a cd
command if you actually need it to run with a particular working directory.
另请注意,终端在不更改工作目录的情况下执行 shell 脚本。cd
如果您确实需要在特定工作目录下运行脚本,则需要使用命令开始脚本。
回答by NobleUplift
回答by Markus Amalthea Magnuson
You can just tell Finder to open the .sh
file in Terminal:
您可以告诉 Finder.sh
在终端中打开文件:
- Select the file
- Get Info (cmd-i) on it
- In the "Open with" section, choose "Other…" in the popup menu
- Choose Terminal as the application
- 选择文件
- 获取信息 (cmd-i)
- 在“打开方式”部分,在弹出菜单中选择“其他...”
- 选择终端作为应用程序
This will have the exact same effect as renaming it to .command
except… you don't have to rename it :)
这将与将其重命名为完全相同的效果,.command
除了……您不必重命名它:)
回答by Manish
- Launch Terminal
- Type -> nano fileName
- Paste Batch file content and save it
- Type -> chmod +x fileName
- It will create exe file now you can double click and it.
- 启动终端
- 类型 -> nano 文件名
- 粘贴批处理文件内容并保存
- 输入 -> chmod +x 文件名
- 它将创建 exe 文件,现在您可以双击它。
File name should in under double quotes. Since i am using Mac->In my case content of batch fileis
文件名应该在双引号下。由于我使用的是 Mac-> 在我的情况下,批处理文件的内容是
cd /Users/yourName/Documents/SeleniumServer
cd /Users/yourName/Documents/SeleniumServer
java -jar selenium-server-standalone-3.3.1.jar -role hub
java -jar selenium-server-standalone-3.3.1.jar -role hub
It will work for sure
它肯定会起作用