Redirecting everything to a file created by an executable?

I have a bunch of executable files, and I want to save the output of each of them in a separate file.

For this purpose, I use the following command, but it "2>&1"does not work every time. And sometimes the output files remain empty, even if the script prints material in the shell when it starts from the shell.

What should i use instead 2>&1?

./$file 2>&1 | tee "$outputFile" 
+3
source share
3 answers

If you want to capture both stdout and stderr, use:

./$file > $outputfile 2>&1

, . , ...

+2

stdout stderr, /dev/tty .

, , psuedo-tty. . script, -, .

+3

, script . , stdout, stderr /dev/tty python script :

brent@battlecruiser:~$ cat test.py
import sys
sys.stdout.write('o\n')
sys.stderr.write('e\n')
with open('/dev/tty', 'w') as tty:
    tty.write('t\n')
brent@battlecruiser:~$ script testout
Script started, file is testout
brent@battlecruiser:~$ python test.py
o
e
t
brent@battlecruiser:~$ exit
Script done, file is testout
brent@battlecruiser:~$ head -n -3 testout | tail -n +3
o
e
t

, , 2>1& tee:

brent@battlecruiser:~$ python test.py 2>&1 | tee testout
e
t
o
brent@battlecruiser:~$ cat testout
e
o

, /dev/tty . script, , , .

+2
source

Source: https://habr.com/ru/post/1779438/


All Articles