From 7a3cb55d8bde9b219bbbffa5fc064f5da39eae1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 19 Oct 2024 18:15:21 +0100 Subject: [PATCH] interp: support closing stdin, stdout, and stderr files --- interp/interp_test.go | 24 ++++++++++++++++++++++++ interp/runner.go | 20 +++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/interp/interp_test.go b/interp/interp_test.go index f67848c1..6462145a 100644 --- a/interp/interp_test.go +++ b/interp/interp_test.go @@ -1329,6 +1329,26 @@ var runTests = []runTest{ "mkdir a && cd a && echo foo_interp_missing >b && cd .. && cat a/b", "foo_interp_missing\n", }, + { + "echo foo 2>&-; :", + "foo\n", + }, + { + // `>&-` closes stdout or stderr. Note that any writes result in errors. + "echo foo >&- 2>&-; :", + "", + }, + { + "echo foo | sed $(read line 2>/dev/null; echo 's/o/a/g')", + "", + }, + { + // `<&-` closes stdin, to e.g. ensure that a subshell does not consume + // the standard input shared with the parent shell. + // Note that any reads result in errors. + "echo foo | sed $(exec <&-; read line 2>/dev/null; echo 's/o/a/g')", + "faa\n", + }, // background/wait {"wait", ""}, @@ -2841,6 +2861,10 @@ done <<< 2`, "read