如何在 Linux Ubuntu 中创建 .sh 扩展文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19289079/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:02:59  来源:igfitidea点击:

How can I create .sh extension file in Linux Ubuntu?

linuxubuntushdaemons

提问by vikas

I'm trying to write a script to run one of my .jarfiles as daemons, but I am not understanding how to create a .shextension file in Ubuntu. I have already used vito create a file with the code that I want, but I cannot figure out how to define the file to specifically be a .sh file. For example, I want to convert my file "foo" or "foo.txt" into "foo.sh".

我正在尝试编写一个脚本来将我的一个.jar文件作为守护程序运行,但我不明白如何.sh在 Ubuntu 中创建扩展文件。我已经使用vi我想要的代码创建了一个文件,但我无法弄清楚如何将文件定义为 .sh 文件。例如,我想将我的文件“ foo”或“ foo.txt”转换为“ foo.sh”。

Is there a simple command I am not seeing that can convert files to a .sh extension or is it a more complicated process?

是否有一个我没有看到的简单命令可以将文件转换为 .sh 扩展名,或者是一个更复杂的过程?

采纳答案by Fredrik Pihl

Use this as a template:

使用它作为模板:

#!/bin/bash

# content of your script

The first line is called a shebangand tells the OS what program that should be used executing the content of the file, in this case, bash.

第一行称为shebang,告诉操作系统应该使用哪个程序来执行文件的内容,在本例中为bash.

To make the file executable:

要使文件可执行:

chmod 755 foo.sh

To execute your script do:

要执行您的脚本,请执行以下操作:

./foo.sh