2024-04-12 18:19:32 +08:00
|
|
|
#RUN: %fish %s
|
2019-10-15 06:45:40 +08:00
|
|
|
|
|
|
|
function outnerr
|
|
|
|
command echo out $argv
|
|
|
|
command echo err $argv 1>&2
|
|
|
|
end
|
|
|
|
|
|
|
|
outnerr 0 &| count
|
|
|
|
#CHECK: 2
|
|
|
|
|
2020-02-17 15:52:41 +08:00
|
|
|
|
|
|
|
outnerr appendfd 2>>&1
|
|
|
|
#CHECK: out appendfd
|
|
|
|
#CHECK: err appendfd
|
|
|
|
|
2019-10-15 06:45:40 +08:00
|
|
|
set -l tmpdir (mktemp -d)
|
2019-12-21 06:40:57 +08:00
|
|
|
outnerr overwrite &>$tmpdir/file.txt
|
2019-10-15 06:45:40 +08:00
|
|
|
cat $tmpdir/file.txt
|
|
|
|
#CHECK: out overwrite
|
|
|
|
#CHECK: err overwrite
|
|
|
|
|
2019-12-21 06:40:57 +08:00
|
|
|
outnerr append &>>$tmpdir/file.txt
|
2019-10-15 06:45:40 +08:00
|
|
|
cat $tmpdir/file.txt
|
|
|
|
#CHECK: out overwrite
|
|
|
|
#CHECK: err overwrite
|
|
|
|
#CHECK: out append
|
|
|
|
#CHECK: err append
|
|
|
|
|
2019-12-21 06:40:57 +08:00
|
|
|
echo noclobber &>>?$tmpdir/file.txt
|
2019-10-15 06:45:40 +08:00
|
|
|
#CHECKERR: {{.*}} The file {{.*}} already exists
|
|
|
|
|
2019-10-28 05:35:14 +08:00
|
|
|
eval "echo foo |& false"
|
|
|
|
#CHECKERR: {{.*}} |& is not valid. In fish, use &| to pipe both stdout and stderr.
|
|
|
|
#CHECKERR: echo foo |& false
|
2022-08-12 00:15:15 +08:00
|
|
|
#CHECKERR: ^^
|
2019-10-28 05:35:14 +08:00
|
|
|
|
2019-12-21 06:40:57 +08:00
|
|
|
# Ensure that redirection empty data still creates the file.
|
|
|
|
rm -f $tmpdir/file.txt
|
|
|
|
test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
|
|
|
|
#CHECK: File does not exist
|
|
|
|
|
|
|
|
echo -n >$tmpdir/file.txt
|
|
|
|
test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
|
|
|
|
#CHECK: File exists
|
|
|
|
|
|
|
|
rm $tmpdir/file.txt
|
|
|
|
echo -n 2>$tmpdir/file.txt
|
|
|
|
test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
|
|
|
|
#CHECK: File exists
|
|
|
|
|
2021-10-22 14:47:48 +08:00
|
|
|
rm $tmpdir/file.txt
|
|
|
|
|
|
|
|
function redir_to_argv1
|
2020-07-15 02:55:38 +08:00
|
|
|
if set -q argv[1]
|
2021-10-22 14:47:48 +08:00
|
|
|
redir_to_argv1 >$argv[1]
|
2020-07-15 02:55:38 +08:00
|
|
|
end
|
|
|
|
echo foo
|
|
|
|
end
|
|
|
|
|
2021-10-22 14:47:48 +08:00
|
|
|
redir_to_argv1 $tmpdir/bar
|
2020-07-15 02:55:38 +08:00
|
|
|
# CHECK: foo
|
|
|
|
cat $tmpdir/bar
|
|
|
|
# CHECK: foo
|
2021-10-22 14:47:48 +08:00
|
|
|
rm $tmpdir/bar
|
2020-07-15 02:55:38 +08:00
|
|
|
|
2020-02-09 00:51:48 +08:00
|
|
|
# Verify that we can turn stderr into stdout and then pipe it
|
|
|
|
# Note that the order here has historically been unspecified - 'errput' could conceivably appear before 'output'.
|
2020-03-10 02:36:12 +08:00
|
|
|
begin
|
|
|
|
echo output
|
|
|
|
echo errput 1>&2
|
2021-07-31 08:21:36 +08:00
|
|
|
end 2>&1 | sort | tee $tmpdir/tee_test.txt
|
|
|
|
cat $tmpdir/tee_test.txt
|
|
|
|
|
2021-10-22 14:47:48 +08:00
|
|
|
rm $tmpdir/tee_test.txt
|
|
|
|
rmdir $tmpdir
|
2021-07-31 08:21:36 +08:00
|
|
|
|
2020-02-09 00:51:48 +08:00
|
|
|
#CHECK: errput
|
|
|
|
#CHECK: output
|
|
|
|
#CHECK: errput
|
|
|
|
#CHECK: output
|
|
|
|
|
|
|
|
# Test that trailing ^ doesn't trigger redirection, see #1873
|
|
|
|
echo caret_no_redirect 12345^
|
|
|
|
#CHECK: caret_no_redirect 12345^
|
|
|
|
|
|
|
|
# Verify that we can pipe something other than stdout
|
|
|
|
# The first line should be printed, since we output to stdout but pipe stderr to /dev/null
|
|
|
|
# The second line should not be printed, since we output to stderr and pipe it to /dev/null
|
2020-03-10 02:36:12 +08:00
|
|
|
begin
|
|
|
|
echo is_stdout
|
|
|
|
end 2>| cat >/dev/null
|
|
|
|
begin
|
|
|
|
echo is_stderr 1>&2
|
|
|
|
end 2>| cat >/dev/null
|
2020-02-09 00:51:48 +08:00
|
|
|
#CHECK: is_stdout
|
|
|
|
|
2021-02-11 09:19:08 +08:00
|
|
|
# Verify builtin behavior with closed stdin.
|
|
|
|
# count silently ignores closed stdin, others may print an error.
|
|
|
|
true <&-
|
|
|
|
echo $status
|
|
|
|
#CHECK: 0
|
|
|
|
test -t 0 <&-
|
|
|
|
echo $status
|
|
|
|
#CHECK: 1
|
|
|
|
read abc <&-
|
|
|
|
#CHECKERR: read: stdin is closed
|
|
|
|
|
2021-10-22 14:47:48 +08:00
|
|
|
# This one should output nothing.
|
|
|
|
echo derp >&-
|
2021-02-11 09:19:08 +08:00
|
|
|
|
2021-10-22 14:47:48 +08:00
|
|
|
# Redirection to 0, 1, 2 should always work. We don't test 0 since writing to stdin is weird and unpredictable.
|
|
|
|
echo hooray1 >&1
|
|
|
|
echo hooray2 >&2
|
|
|
|
#CHECK: hooray1
|
|
|
|
#CHECKERR: hooray2
|
2021-02-11 09:19:08 +08:00
|
|
|
|
2020-02-09 00:51:48 +08:00
|
|
|
# "Verify that pipes don't conflict with fd redirections"
|
2021-03-04 05:23:43 +08:00
|
|
|
# This code is very similar to eval. We go over a bunch of fads
|
2020-02-09 00:51:48 +08:00
|
|
|
# to make it likely that we will nominally conflict with a pipe
|
|
|
|
# fish is supposed to detect this case and dup the pipe to something else
|
|
|
|
echo "/bin/echo pipe 3 <&3 3<&-" | source 3<&0
|
|
|
|
echo "/bin/echo pipe 4 <&4 4<&-" | source 4<&0
|
|
|
|
echo "/bin/echo pipe 5 <&5 5<&-" | source 5<&0
|
|
|
|
echo "/bin/echo pipe 6 <&6 6<&-" | source 6<&0
|
|
|
|
echo "/bin/echo pipe 7 <&7 7<&-" | source 7<&0
|
|
|
|
echo "/bin/echo pipe 8 <&8 8<&-" | source 8<&0
|
|
|
|
echo "/bin/echo pipe 9 <&9 9<&-" | source 9<&0
|
2021-03-04 05:23:43 +08:00
|
|
|
echo "/bin/echo pipe 10 <&10 10<&-" | source 10<&0
|
|
|
|
echo "/bin/echo pipe 11 <&11 11<&-" | source 11<&0
|
|
|
|
echo "/bin/echo pipe 12 <&12 12<&-" | source 12<&0
|
2020-02-09 00:51:48 +08:00
|
|
|
#CHECK: pipe 3
|
|
|
|
#CHECK: pipe 4
|
|
|
|
#CHECK: pipe 5
|
|
|
|
#CHECK: pipe 6
|
|
|
|
#CHECK: pipe 7
|
|
|
|
#CHECK: pipe 8
|
|
|
|
#CHECK: pipe 9
|
2021-03-04 05:23:43 +08:00
|
|
|
#CHECK: pipe 10
|
|
|
|
#CHECK: pipe 11
|
|
|
|
#CHECK: pipe 12
|
2021-11-20 04:11:28 +08:00
|
|
|
|
|
|
|
echo foo >/bin/echo/file
|
|
|
|
#CHECKERR: warning: An error occurred while redirecting file '/bin/echo/file'
|
|
|
|
#CHECKERR: warning: Path '/bin/echo' is not a directory
|
2024-03-20 23:44:59 +08:00
|
|
|
|
|
|
|
echo foo <?nonexistent
|
|
|
|
#CHECK: foo
|
|
|
|
echo $status
|
|
|
|
#CHECK: 0
|
|
|
|
|
|
|
|
read -l foo <?nonexistent
|
|
|
|
echo $status
|
|
|
|
#CHECK: 1
|
|
|
|
set -S foo
|
|
|
|
#CHECK: $foo: set in local scope, unexported, with 0 elements
|
|
|
|
|
|
|
|
set -l fish (status fish-path)
|
|
|
|
$fish --no-config -c 'true <&?fail'
|
|
|
|
#CHECKERR: fish: Requested redirection to '?fail', which is not a valid file descriptor
|
|
|
|
#CHECKERR: true <&?fail
|
|
|
|
#CHECKERR: ^~~~~~^
|
|
|
|
|
|
|
|
$fish --no-config -c 'true <?&fail'
|
|
|
|
#CHECKERR: fish: Expected a string, but found a '&'
|
|
|
|
#CHECKERR: true <?&fail
|
|
|
|
#CHECKERR: ^
|