Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Docker robustly #14

Merged
merged 1 commit into from
Sep 12, 2023
Merged

Detect Docker robustly #14

merged 1 commit into from
Sep 12, 2023

Conversation

a4lg
Copy link
Contributor

@a4lg a4lg commented Sep 8, 2023

&> /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.

"&> /dev/null" works as a redirection from stdout/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 (makes it possible
to detect "no Docker" condition correctly).

Signed-off-by: Tsukasa OI <research_trasio@irq.a4lg.com>
@a4lg
Copy link
Contributor Author

a4lg commented Sep 10, 2023

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!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants