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

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

问题描述

首先,我正在使用Mac.

First off I'm using Mac.

接下来,我需要执行此"file.sh",我们将其称为.每次我需要执行它时,我都必须打开Terminal并输入:

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?

哪种方法最好,或者还有更好的方法?另外,请您尽可能多地解释一下,我是Terminal的新手.谢谢.

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脚本,请使用命令"扩展名将其命名,例如file.command.默认情况下,这些文件将发送到Terminal,Terminal将作为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.

请注意,在此特定方案中,脚本不必以#!前缀开头,因为Terminal专门安排使用您的默认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.)

还请注意,Terminal在不更改工作目录的情况下执行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天全站免登陆