Сценарий Bash, не работающий с 'sh'

Также проверьте свой локальный/etc/hosts файл. Это обычно имеет приоритет по вызовам DNS. Когда Вы, SSH через имя хоста и имя хоста существует в файле hosts, это войдет в систему с этим. Ваш рыть команду запрашивает сервер DNS непосредственно, обходя файл hosts.

3
задан 22 June 2012 в 01:08
2 ответа

Summary

sh is a different program than bash.

Detail

The problem is that the Bourne shell (sh) is not the Bourne Again shell (bash). Namely, sh doesn't understand the [[ pragma. In fact, it doesn't understand [ either. [ is an actual program or link to /bin/test (or /usr/bin/[, /usr/bin/test).

$ which [
/bin/[
$ ls -lh /bin/[
-r-xr-xr-x  2 root  wheel    42K Feb 29 17:11 /bin/[

When you execute your script directly through ./test.sh, you're calling the script as the first argument to the program specified in the first line. In this case:

#!/usr/bin/env bash

Often, this is directly the interpreter (/bin/bash, or any number of other script interpreters), but in your case you're using env to run a program in a modified environment -- but that follow argument is still bash. Effectively, ./test.sh is bash test.sh.

Because sh and bash are different shells with different syntax interpretations, you're seeing that error. If you run bash test.sh, you should see what is expected.

More info

Others have pointed out in comments that /bin/sh can be a link or other shell. Historically, sh was the Bourne shell on the old AT&T Unix, and in my mind the canonical descent. However, that is different in BSD variations and has diverged in other Unix based systems and distributions over time. If you're really interested in the inner workings (including how /bin/sh and /bin/bash can be the same program and behave totally differently), read the following:

SuperUser: What is the difference between bash and sh

Wikipedia: Bourne shell

15
ответ дан 3 December 2019 в 04:38

Как уже отмечалось: / bin / sh обычно (хотя и не всегда) запускает POSIX-совместимую оболочку Bourne. Bash - это не Bourne.

Bash будет пытаться подражать Bourne при вызове как 'sh' (например, когда / bin / sh символические ссылки или ссылки на / bin / bash), или если $ POSIXLY_CORRECT определен в вызывающей среде при вызове с параметром вызова --posix или при выполнении 'set -o posix'. Это позволяет тестировать сценарий / команду оболочки Bourne на соответствие POSIX.

В качестве альтернативы можно вызывать сценарии / тестовые команды с известной POSIX-совместимой оболочкой. «тире» близко, оболочка Korn (ksh) IIRC также предлагает вариант, совместимый с POSIX.

3
ответ дан 3 December 2019 в 04:38

Теги

Похожие вопросы