Pregunta

Let us consider there are many system commands inside a shell script with each of them returning some content to stdout or stderr.

Instead of performing redirection for each and every command separately is there any way to redirect all the stdout or stderr generated from the shell script to a log file ?

¿Fue útil?

Solución

the obviously simple solution would be to use

./scriptfile.sh > foo.log

thus all stdout generated withing the scriptfile goes to foo.log

i guess however, that your question is directed towards a solution that works from withing the script. you can (re)direct a file-descriptor to a given file using the exec command (line 2 in the following snippet will redirect stdout to foo.log):

#!/bin/sh
exec 1>>foo.log

echo blue
echo blart
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top