riscv / docs-spec-template

https://lists.riscv.org/g/sig-documentation
Creative Commons Attribution 4.0 International
18 stars 19 forks source link

Detect Docker robustly #14

Closed a4lg closed 10 months ago

a4lg commented 10 months ago

&> /dev/null works as a redirection from stdout and stderr to /dev/null in Bash but not in POSIX shell (considered as an asynchronous execution and the result of the command command cannot be retrieved).

As a result, it always assumes that Docker always exists.

This commit makes the redirection robust and portable (uses >/dev/null 2>&1 instead, makes it possible to detect "no Docker" condition correctly).

I didn't meant to do that but it seems this commit also appends a new line character to Makefile's EOF.

a4lg commented 10 months ago

Important Note

Whether &> /dev/null works as a redirection from stdout and stderr to /dev/null depends on the /bin/sh installed on the system.

Quick Test

Makefile

all:
    ./sample.sh &> /dev/null; echo $$?
.PHONY: all

sample.sh

#! /bin/sh
sleep 10
echo TO_STDOUT
echo TO_STDERR 1>&2
exit 1

Test Results

Fedora 38, CentOS and many Red Hat-based Linux distributions

/bin/sh is Bash.

&> /dev/null works as a redirection as the developer intended. Running make prints 1 after ten seconds.

Ubuntu 23.04 and many Debian-based Linux distributions

/bin/sh is Dash (Debian Almquist Shell), a lightweight shell interpreter which conforms to POSIX and implements a very few extensions (compared to Bash or Zsh).

&> /dev/null does not work as intended. Running make prints 0 immediately and TO_STDOUT and TO_STDERR (both should be redirected) are printed to the console after ten seconds (and even after make exits!).