如何在Unix控制台或Mac终端上运行Shell脚本? [英] How to run a shell script on a Unix console or Mac terminal?

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

问题描述

我知道,忘记了,然后重新学习.是时候写下来了.

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

要运行不可执行的bash脚本,请使用:

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

当文件是可执行文件时,内核负责确定如何执行该文件.对于非二进制文件,这可以通过查看文件的第一行来完成.它应该包含一个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与参数bash一起运行).然后,脚本和您为脚本提供的所有参数一起作为后续参数传递给程序(作为第二个参数).

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.

这意味着每个可执行脚本都应具有哈希键.如果不是,则您不会告诉内核是什么,因此内核不知道使用什么程序来解释它.可能是bashperlpythonsh或其他名称. (实际上,内核通常会使用用户的默认外壳程序来解释文件,这是非常危险的,因为它可能根本不是正确的解释器,或者它可能能够解析其中的某些文件,但是行为上却存在细微差别,例如shbash之间的情况).

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

结果是内核将运行程序/bin/bash来解释脚本.遗憾的是,bash并非默认情况下始终出厂,并且在/bin中并非始终可用.在Linux机器上通常是这样,bash在其他位置(例如/usr/xpg/bin/bash/usr/local/bin/bash)也有很多其他POSIX机器.

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脚本,我们不能依靠对bash程序的位置进行硬编码. POSIX已经具有处理该问题的机制:PATH.想法是,将程序安装在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.幸运的是,几乎所有系统都在/usr/bin中安装了env程序.因此,我们使用硬编码的路径开始env,该路径随后在PATH中搜索bash并运行它,以便它可以解释您的脚本:

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,哈希爆炸可以具有一个参数.在这种情况下,我们将bash用作env程序的参数.这意味着我们没有空间将参数传递给bash.因此,无法将#!/bin/bash -exu之类的内容转换为该方案.您必须将set -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.

此方法还具有另一个优点:某些系统可能附带/bin/bash,但用户可能不喜欢它,可能发现它有故障或过时,并且可能在其他地方安装了自己的bash.在OS X(Macs)上通常是这种情况,其中Apple发行了过时的/bin/bash,并且用户使用Homebrew之类的东西安装了最新的/usr/local/bin/bash.当使用env方法进行PATH搜索时,您会考虑用户的偏好,并使用他的首选bash而不是系统附带的bash.

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终端上运行Shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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