1#RUN: %fish %s
2
3function outnerr
4    command echo out $argv
5    command echo err $argv 1>&2
6end
7
8outnerr 0 &| count
9#CHECK: 2
10
11
12outnerr appendfd 2>>&1
13#CHECK: out appendfd
14#CHECK: err appendfd
15
16set -l tmpdir (mktemp -d)
17outnerr overwrite &>$tmpdir/file.txt
18cat $tmpdir/file.txt
19#CHECK: out overwrite
20#CHECK: err overwrite
21
22outnerr append &>>$tmpdir/file.txt
23cat $tmpdir/file.txt
24#CHECK: out overwrite
25#CHECK: err overwrite
26#CHECK: out append
27#CHECK: err append
28
29echo noclobber &>>?$tmpdir/file.txt
30#CHECKERR: {{.*}} The file {{.*}} already exists
31
32eval "echo foo |& false"
33#CHECKERR: {{.*}} |& is not valid. In fish, use &| to pipe both stdout and stderr.
34#CHECKERR: echo foo |& false
35#CHECKERR:          ^
36
37# Ensure that redirection empty data still creates the file.
38rm -f $tmpdir/file.txt
39test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
40#CHECK: File does not exist
41
42echo -n >$tmpdir/file.txt
43test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
44#CHECK: File exists
45
46rm $tmpdir/file.txt
47echo -n 2>$tmpdir/file.txt
48test -f $tmpdir/file.txt && echo "File exists" || echo "File does not exist"
49#CHECK: File exists
50
51function foo
52    if set -q argv[1]
53        foo >$argv[1]
54    end
55    echo foo
56end
57
58foo $tmpdir/bar
59# CHECK: foo
60cat $tmpdir/bar
61# CHECK: foo
62
63rm -Rf $tmpdir
64
65# Verify that we can turn stderr into stdout and then pipe it
66# Note that the order here has historically been unspecified - 'errput' could conceivably appear before 'output'.
67begin
68    echo output
69    echo errput 1>&2
70end 2>&1 | sort | tee ../test/temp/tee_test.txt
71cat ../test/temp/tee_test.txt
72#CHECK: errput
73#CHECK: output
74#CHECK: errput
75#CHECK: output
76
77# Test that trailing ^ doesn't trigger redirection, see #1873
78echo caret_no_redirect 12345^
79#CHECK: caret_no_redirect 12345^
80
81# Verify that we can pipe something other than stdout
82# The first line should be printed, since we output to stdout but pipe stderr to /dev/null
83# The second line should not be printed, since we output to stderr and pipe it to /dev/null
84begin
85    echo is_stdout
86end 2>| cat >/dev/null
87begin
88    echo is_stderr 1>&2
89end 2>| cat >/dev/null
90#CHECK: is_stdout
91
92# Verify builtin behavior with closed stdin.
93# count silently ignores closed stdin, others may print an error.
94true <&-
95echo $status
96#CHECK: 0
97test -t 0 <&-
98echo $status
99#CHECK: 1
100read abc <&-
101#CHECKERR: read: stdin is closed
102
103
104
105# "Verify that pipes don't conflict with fd redirections"
106# This code is very similar to eval. We go over a bunch of fads
107# to make it likely that we will nominally conflict with a pipe
108# fish is supposed to detect this case and dup the pipe to something else
109echo "/bin/echo pipe 3 <&3 3<&-" | source 3<&0
110echo "/bin/echo pipe 4 <&4 4<&-" | source 4<&0
111echo "/bin/echo pipe 5 <&5 5<&-" | source 5<&0
112echo "/bin/echo pipe 6 <&6 6<&-" | source 6<&0
113echo "/bin/echo pipe 7 <&7 7<&-" | source 7<&0
114echo "/bin/echo pipe 8 <&8 8<&-" | source 8<&0
115echo "/bin/echo pipe 9 <&9 9<&-" | source 9<&0
116echo "/bin/echo pipe 10 <&10 10<&-" | source 10<&0
117echo "/bin/echo pipe 11 <&11 11<&-" | source 11<&0
118echo "/bin/echo pipe 12 <&12 12<&-" | source 12<&0
119#CHECK: pipe 3
120#CHECK: pipe 4
121#CHECK: pipe 5
122#CHECK: pipe 6
123#CHECK: pipe 7
124#CHECK: pipe 8
125#CHECK: pipe 9
126#CHECK: pipe 10
127#CHECK: pipe 11
128#CHECK: pipe 12
129