如何运行在UNIX控制台/ Mac的终端.SH脚本? [英] How to run a .sh-script in an Unix console/Mac terminal?

查看:152
本文介绍了如何运行在UNIX控制台/ Mac的终端.SH脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,忘记它,再重新学习它。时间把它写下来。

I know it, forget it and relearn it again. Time to write it down.

推荐答案

要运行一个非执行 SH 脚本,使用:

To run a non-executable sh script, use:

sh myscript

要运行一个非执行庆典脚本,使用:

To run a non-executable bash script, use:

bash myscript

要启动一个可执行文件(这是与可执行权限的任何文件);您只需通过它的路径指定它:

To start an executable (which is any file with executable permission); you just specify it by its path:

/foo/bar
/bin/bar
./bar

要做出一个可执行脚本,给它必要的权限:

To make a script executable, give it the necessary permission:

chmod +x bar
./bar

当一个文件是可执行文件,在内核负责搞清楚如何execte它。用于非二进制文件,这是通过查看该文件的第一行进行。它应该包含 hashbang

When a file is executable, the kernel is responsible for figuring out how to execte it. For non-binaries, this is done by looking at the first line of the file. It should contain a hashbang:

#! /usr/bin/env bash

该hashbang告诉内核运行什么程序(在这种情况下,命令的/ usr / bin中/ env的正在运行的参数庆典)。然后,脚本传递给程序(第二个参数)的所有参数沿着你给脚本作为后续参数。

The hashbang tells the kernel what program to run (in this case the command /usr/bin/env is ran with the argument bash). Then, the script is passed to the program (as second argument) along with all the arguments you gave the script as subsequent arguments.

这是用于每个脚本是可执行应该有一个hashbang 。如果没有,你不告诉内核是什么的的,因此内核不知道用国米$ P $私人它什么程序。这可能是庆典 perl的蟒蛇 SH ,或别的东西。 (在现实中,内核会经常使用用户的默认外壳间preTE文件,这是非常危险的,因为它可能不是在所有正确的跨preTER或者它可能是能够解析它的一些但细微的行为差异,如之间的情况下, SH 庆典)。

That means every script that is executable should have a hashbang. If it doesn't, you're not telling the kernel what it is, and therefore the kernel doesn't know what program to use to interprete it. It could be bash, perl, python, sh, or something else. (In reality, the kernel will often use the user's default shell to interprete the file, which is very dangerous because it might not be the right interpreter at all or it might be able to parse some of it but with subtle behavioural differences such as is the case between sh and bash).

最常见的,你会看到散的刘海像这样:

Most commonly, you'll see hash bangs like so:

#!/bin/bash

其结果是,内核将运行程序 /斌/庆典国米preT脚本。不幸的是,庆典并不总是默认出厂,这是不是总是可用 / bin中。虽然在Linux机器上通常是,还有其他的POSIX的机器的范围,其中庆典船舶在不同的地点,如的/ usr / XPG /箱/ bash的的/ usr / local / bin目录/ bash的

The result is that the kernel will run the program /bin/bash to interpret the script. Unfortunately, bash is not always shipped by default, and it is not always available in /bin. While on Linux machines it usually is, there are a range of other POSIX machines where bash ships in various locations, such as /usr/xpg/bin/bash or /usr/local/bin/bash.

要编写一个可移植bash脚本,因此,我们可以不依赖于硬编码的庆典程序的位置。 POSIX已经为处理一个机制: PATH 。这个想法是,你是在路径中的目录键,系统应该能够找到你的程序,当你想要的名字。要运行一个安装程序

To write a portable bash script, we can therefore not rely on hard-coding the location of the bash program. POSIX already has a mechanism for dealing with that: PATH. The idea is that you install your programs in one of the directories that are in PATH and the system should be able to find your program when you want to run it by name.

可悲的是,您不能只是这样做:

Sadly, you cannot just do this:

#!bash

内核不会(有些人可能会)做了 PATH 搜索你。有一个程序,可以做一个 PATH 搜索你,虽然,这就是所谓的 ENV 。幸运的是,几乎所有的系统都具有 ENV安装在的/ usr / bin中 程序。因此,我们开始使用硬codeD路径,然后做了 ENV PATH 搜索庆典并运行它,这样它可以跨preT你的脚本:

The kernel won't (some might) do a PATH search for you. There is a program that can do a PATH search for you, though, it's called env. Luckily, nearly all systems have an env program installed in /usr/bin. So we start env using a hardcoded path, which then does a PATH search for bash and runs it so that it can interpret your script:

#!/usr/bin/env bash

这种方法有一个缺点:根据POSIX,在hashbang可以有一个参数即可。在这种情况下,我们使用庆典作为参数传递给 ENV 程序。这意味着我们没有空间留给参数传递给庆典。因此,有没有办法转换成类似#!/斌/ bash的-exu 该方案。你必须把设置-exu 后hashbang来代替。

This approach has one downside: According to POSIX, the hashbang can have one argument. In this case, we use bash as the argument to the env program. That means we have no space left to pass arguments to bash. So there's no way to convert something like #!/bin/bash -exu to this scheme. You'll have to put set -exu after the hashbang instead.

这方法也有一个好处:一些系统可能以 /斌/庆典出货,但用户可能不喜欢它,会发现它的越野车或过时了,可能已经安装了自己的庆典别的地方。这往往是在OS X(Mac机),其中苹果船舶过时 /斌/庆典键,用户安装了最新的 / USR的情况下/ local / bin目录/ bash的使用类似家酿。当您使用 ENV 的做法,做了 PATH 搜索,你把用户的preference考虑和使用他的preferred庆典在他的一个系统附带。

This approach also has another advantage: Some systems may ship with a /bin/bash, but the user may not like it, may find it's buggy or outdated, and may have installed his own bash somewhere else. This is often the case on OS X (Macs) where Apple ships an outdated /bin/bash and users install an up-to-date /usr/local/bin/bash using something like Homebrew. When you use the env approach which does a PATH search, you take the user's preference into account and use his preferred bash over the one his system shipped with.

这篇关于如何运行在UNIX控制台/ Mac的终端.SH脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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