shell command 에러 출력 막는 방법에 대한 문의

ssanighe의 이미지

[root@ssanighe]# cat test/vmstat.txt
cat: test/vmstat.txt: No such file or directory
 
[root@ssanighe]# cat test/vmstat.txt >/dev/null 2>&1
[root@ssanighe]#

위의 예제를 보시면, 에러를 출력하지 않기 위해
stdout 과 stderr 을 /dev/null 로 보내버렸습니다.

그런데, 아래 예제처럼 "test" 디렉토리가 없을때,
에러를 출력하지 않으려면 어떻게 해야 될지요..

궁굼합니다.

[root@ssanighe]# vmstat > test/vmstat.txt
-bash: test/vmstat.txt: No such file or directory
[root@ssanighe]# vmstat > test/vmstat.txt 2>/dev/null
-bash: test/vmstat.txt: No such file or directory

ktd2004의 이미지

원하시는 답이 아닐지도 모르지만 다음과 같은 방법도 있습니다.
test 디렉토리가 있을 때만 뒤쪽 명령이 실행됩니다.

[ -d test ] && vmstat > test/vmstat.txt
bushi의 이미지

vmstat 2>/dev/null > test/vmstat.txt
ssanighe의 이미지

브라보!