如何通过双击使这个 file.sh 可执行? [英] How do I make this file.sh executable via double click?

查看:24
本文介绍了如何通过双击使这个 file.sh 可执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我使用的是 Mac.

First off I'm using Mac.

接下来,我需要执行这个file.sh",我们将调用它.每次我需要执行它时,我都必须打开终端并输入:

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:

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?

我的想法是:

a) 在终端中输入类似 chmod 的内容并更改权限?

a) type something like chmod into terminal and change permissions?

b) 创建一个文件,将我上面写的代码放入其中 ^ 然后使该文件可执行?

b) make a file, put code I wrote above in it ^ and then make that file executable?

c) 以某种方式进行自动化来做到这一点?

c) make an automation somehow to do this?

哪种方式最好,或者有更好的方式?另外请尽可能多地解释,我是终端的新手.谢谢.

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.

推荐答案

默认情况下,*.sh 文件在文本编辑器(Xcode 或 TextEdit)中打开.要创建在终端打开时将在终端中执行的 shell 脚本,请使用command"扩展名命名它,例如 file.command.默认情况下,这些会发送到终端,终端会将该文件作为 shell 脚本执行.

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.

您还需要确保文件是可执行的,例如:

You will also need to ensure the file is executable, e.g.:

chmod +x file.command

没有这个,终端将拒绝执行它.

Without this, Terminal will refuse to execute it.

请注意,在此特定场景中,脚本不必以 #! 前缀开头,因为终端专门安排使用您的默认 shell 执行它.(当然,如果您想自定义使用哪个 shell,或者如果您想确保在使用不同的 shell 时可以从命令行执行它,您可以添加 #! 行.)

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 脚本.如果您确实需要在特定工作目录下运行脚本,则需要使用 cd 命令开始脚本.

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.

这篇关于如何通过双击使这个 file.sh 可执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆