Tente isto :
echo "test" 1>STDOUT 2>STDERR
Substitua echo "test"
por qualquer comando ou script.
Exemplo simples :
Crie script.sh com conteúdo:
#!/bin/bash
du -shc /*
Adicione permissão de execução:
chmod u+x script.sh
E execute:
./script.sh 1>STDOUT 2>STDERR
Depois, veja cada arquivo:
# cat STDOUT
8,6M /bin
39M /boot
0 /dev
4,1M /etc
1,1G /home
0 /initrd.img
0 /initrd.img.old
231M /lib
4,0K /lib64
# cat STDERR
du: cannot access './proc/7422/task/7422/fd/4': No such file or directory
du: cannot access './proc/7422/task/7422/fdinfo/4': No such file or directory
du: cannot access './proc/7422/fd/4': No such file or directory
du: cannot access './proc/7422/fdinfo/4': No such file or directory
Para configurar o redirecionamento dentro do script, use exec
:
#!/bin/bash
exec 1>STDOUT 2>STDERR
du -shc /*
E simplesmente execute o script:
./script.sh
Explicação :
1>filename
Redirect stdout to file "filename."
1>>filename
Redirect and append stdout to file "filename."
2>filename
Redirect stderr to file "filename."
2>>filename
Redirect and append stderr to file "filename."
&>filename
Redirect both stdout and stderr to file "filename."
This operator is now functional, as of Bash 4, final release.
M>N
"M" is a file descriptor, which defaults to 1, if not explicitly set.
"N" is a filename.
File descriptor "M" is redirect to file "N."
M>&N
"M" is a file descriptor, which defaults to 1, if not set.
"N" is another file descriptor.
Para obter mais informações, consulte Redirecionamento de E / S