Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash vs sh

12 views
Skip to first unread message

wg

unread,
Aug 18, 2018, 11:28:36 AM8/18/18
to
Hallo Leute,

hier ist eine openSUSE Leap 42.3
die Shell /bin/sh zeigt auf /usr/bash
lrwxrwxrwx 1 root root 4 May 26 11:17 /bin/sh -> bash
file /bin/sh: symbolic link to bash


ich hab ein shell Script 'x.sh' mit explizitem Aufruf '#!/bin/bash', das
stdout+stderr auf die Konsole und ins file xxx.log ausgibt:

#!/bin/bash
exec 1> >(tee -a ./xxx.log) 2>&1
echo "$(date)"

Geht wie beabsichtigt.


Wenn ich aber #!/bin/sh verwende
#!/bin/sh
exec 1> >(tee -a ./xxx.log) 2>&1
echo "$(date)"

gibts den Fehler:
monitor@MGTW:~> x.sh
./x.sh: line 2: syntax error near unexpected token `>'
./x.sh: line 2: `exec 1> >(tee -a ./xxx.log) 2>&1'
monitor@MGTW:~>

strace ist identisch bis zur Zeile 181, ab da läufts anders - wieso?
strace /bin/sh
...
read(255, "#!/bin/sh \nexec 1> >(tee -a ./xx"..., 60) = 60
open("/usr/share/locale-langpack/en_US.UTF-8/LC_MESSAGES/bash.mo",
O_RDONLY) = -1 ENOENT (No such file or directory)
...
strace /bin/bash
read(255, "#!/bin/bash \nexec 1> >(tee -a ./"..., 62) = 62
rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0
...

Aufgrund des symlink von /bin/sh auf /bin/bash dachte ich, daß anstelle
von /bin/sh /bin/bash verwendet wird...

Kein großes Ding, aber hat jemand dafür eine Erklärung?

Danke fürs Lesen
Wolf

Markus Koßmann

unread,
Aug 18, 2018, 1:36:58 PM8/18/18
to
wg wrote:

> Hallo Leute,
>
> hier ist eine openSUSE Leap 42.3
> die Shell /bin/sh zeigt auf /usr/bash
> lrwxrwxrwx 1 root root 4 May 26 11:17 /bin/sh -> bash
> file /bin/sh: symbolic link to bash
>
>
> ich hab ein shell Script 'x.sh' mit explizitem Aufruf '#!/bin/bash', das
> stdout+stderr auf die Konsole und ins file xxx.log ausgibt:
>
> #!/bin/bash
> exec 1> >(tee -a ./xxx.log) 2>&1
> echo "$(date)"
>
> Geht wie beabsichtigt.
[...]
>
>
> Kein großes Ding, aber hat jemand dafür eine Erklärung?
>
Aus der man page:
If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well. When invoked as an interactive
login shell, or a non-interactive shell with the --login option,it first
attempts to read and execute commands from /etc/profile and ~/.profile,
in that order. The --noprofile option may be used to inhibit this
behavior. When invoked as an interactive shell with the name sh, bash
looks for the variable ENV, expands its value if it is defined, and uses the
expanded value as the name of a file to read and execute. Since a shell
invoked as sh does not attempt to read and execute commands from any other
startup files, the --rcfile option has no effect. A non-interactive shell
invoked with the name sh does not attempt to read any other startup files.
When invoked as sh, bash enters posix mode after the startup files are
read.

Wenn du z.B. in deinem Script eine Konstrukt verwendest, das von bash im
posix mode nicht unterstützt wird gibt es solche Fehler.




wg

unread,
Aug 19, 2018, 6:29:43 AM8/19/18
to
On 18.08.2018 19:36, Markus Koßmann wrote:
> When invoked as sh, bash enters posix mode after the startup files are
> read.
Danke, das hab ich überlesen, bzw. soweit hab ich nicht gescrollt :-/

Scönens Wochenende
Wolf

Bernd Petrovitsch

unread,
Aug 24, 2018, 5:17:03 AM8/24/18
to
On Sun, 19 Aug 2018 12:29:41 +0200, wg wrote:
> On 18.08.2018 19:36, Markus Koßmann wrote:
>> When invoked as sh, bash enters posix mode after the startup files are
>> read.
> Danke, das hab ich überlesen, bzw. soweit hab ich nicht gescrollt :-/

I.Ü. ist es auich keine gute Idee (und natürlich technisch falsch),
bash-isms in einem Shell-Script zu verwenden und es mit "#!/bin/sh"
starten zu wollen, weil es keine Garantie gibt, daß /bin/sh eine
bash ist.
Das mag bei Dir/Suse-Welt so sein, das mag in der RedHat-Welt so
sein, aber in der Debian/Ubuntu-Welt geht das garaniert schief
(weil /bin/sh dort die /bin/dash ist).

Von busybox-only Systemen ganz zu schweigen;-)

MfG,
Bernd
0 new messages