문제

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 ?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top