bash 如何为终端创建新的 unix 可执行文件 (.sh?) 以用作命令?

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

How do I create new unix executable (.sh?) files for the terminal to use as commands?

bashunix

提问by Sergey Brin

I'd like to make some files to put in my /usr/bin folder (Mac OS) and be able to run them by typing the name to the terminal, like the commands gcc, cd, or vim(those are all in that folder). When I open such a file as text, it appears encrypted, so I'm not sure how to create one or what extension to use. Thank you.

我想将一些文件放入我的 /usr/bin 文件夹(Mac OS)中,并能够通过在终端中输入名称来运行它们,例如命令gcccdvim(这些都在里面文件夹)。当我打开这样一个文本文件时,它看起来是加密的,所以我不确定如何创建一个或使用什么扩展名。谢谢你。

回答by PressingOnAlways

The files you see in /usr/binare not encrypted - they're compiled code in machine language different from bash scripts. You can however have scripts also in the /usr/binlocation and have them run exactly as you are expecting.

您在其中看到的文件/usr/bin没有加密——它们是用不同于 bash 脚本的机器语言编译的代码。但是,您也可以在该/usr/bin位置放置脚本,并让它们完全按照您的预期运行。

In order to do that, you will have to create an executable script file. In unix, scripts are not identified by file extension but by 2 things:

为此,您必须创建一个可执行脚本文件。在 unix 中,脚本不是通过文件扩展名来标识的,而是通过两件事来标识的:

  1. executable bit set on the file permission level
  2. Script interpreter header, such as #!/bin/bash
  1. 在文件权限级别设置的可执行位
  2. 脚本解释器标头,例如 #!/bin/bash

For a bash script, you can do the following:

对于 bash 脚本,您可以执行以下操作:

Make a new file with the following content:

创建一个包含以下内容的新文件:

#!/bin/bash

echo "Hello world - My first bash script."

Save the file as hello.sh(the .sh is just convention, it could be any file name).

将文件另存为hello.sh(.sh 只是约定,它可以是任何文件名)。

Then run chmod +x hello.shand you will be able to run this file as an executable.

然后运行chmod +x hello.sh,您将能够将此文件作为可执行文件运行。

Move this file to /usr/local/binand you should be able to run hello.shfrom command line and it should execute your program.

将此文件移动到/usr/local/bin您应该能够从命令行运行hello.sh并且它应该执行您的程序。

回答by Pritam Pan

Programs such as gccand cdare not encrypted, but compiled; however, if you create a shell script (without .shas suffix; the .shfile extension for a shell script is optional) and place it in /usr/binor any PATH location, and you chmod +x <script-path>(to give execute permission) then you can directly use that as a command.

诸如gcc和 之cd类的程序未加密,而是经过编译;但是,如果您创建一个 shell 脚本(没有.sh作为后缀;shell 脚本的.sh文件扩展名是可选的)并将其放置在/usr/binPATH 或任何位置,并且您chmod +x <script-path>(授予执行权限)那么您可以直接将其用作命令。

Note: use shebangto mention the script interpreter, e.g. #!/usr/bin/env bashor equivalent for korn shell, python, etc.

注意:使用shebang来提及脚本解释器,例如#!/usr/bin/env bashkorn shell、python 等的等价物。

回答by Azize

You can create scripts, for example in bash, make the file executable with chmodand put the file path in your $PATH.

您可以创建脚本,例如在 bash 中,使文件可执行chmod并将文件路径放在 .bashrc 中$PATH

Example: Create a new file called myscript.sh, you can use vior any editor you prefer. Add the content below on it:

示例:创建一个名为 的新文件myscript.sh,您可以使用它vi或您喜欢的任何编辑器。在其上添加以下内容:

#!/bin/bash

echo "Hello world!"

Now make it executable:

现在使它可执行:

chmod u+x my script.sh

You can run your script like this ./myscript.shor add it to your path:

您可以像这样运行脚本./myscript.sh或将其添加到您的路径中:

export PATH=$PATH:$PWD

Now you can just run with the name, like myscript.sh

现在您可以使用名称运行,例如 myscript.sh