`time command`에서 명령의 출력을 재 지정하십시오 /dev/null하지만 여전히 시간 출력을 stderr에 기록하는

나는 다음을 사용하여 무언가를 시도하고있다 :

/usr/bin/time myCommand

그러나 /usr/bin/timestderr에 쓰기 때문에 myCommand 도 stderr에 쓰기하면 스트림에서 시간의 출력 이상을 얻습니다. 내가하고 싶은 것은 모든 myCommand의 출력을로 리디렉션 /dev/null하지만 여전히 시간 출력을 stderr에 기록하는 것입니다. stderr에 쓰는 myCommand 예제를 사용하면 ls /nofile다음과 같은 결과가 전혀 없다는 것을 알 수 있습니다.

$ /usr/bin/time ls /nofile 2> /dev/null
$

리디렉션이 없으면 ls(stderr로)의 출력과 시간으로 (stderr로) 의 출력을 모두 볼 수 있습니다 .

$ /usr/bin/time ls /nofile
ls: cannot access /nofile: No such file or directory
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3776maxresident)k
0inputs+0outputs (0major+278minor)pagefaults 0swaps

내가 원하는 것은 단순히 생산하는 것입니다.

$ /usr/bin/time ls /nofile > RedirectThatImAskingFor
0.00user 0.00system 0:00.00elapsed 0%CPU (0avgtext+0avgdata 3776maxresident)k
0inputs+0outputs (0major+278minor)pagefaults 0swaps

어떤 아이디어?



답변

ksh에서 bash 및 zsh time는 내장 키워드가 아닌 키워드입니다. 같은 줄의 리디렉션은 time자체 출력이 아닌 시간이 지정된 명령에만 적용됩니다 .

$ time ls -d / /nofile >/dev/null 2>/dev/null

real    0m0.003s
user    0m0.000s
sys     0m0.000s

time이러한 셸에서 출력 자체 를 리디렉션하려면 추가 수준의 그룹화를 사용해야합니다.

{ time mycommand 2>&3; } 3>&2 2>mycommand.time

독립형 time유틸리티 의 GNU 버전을 사용하는 경우 stderr 이외 -o의 출력을 작성 하는 옵션이 time있습니다. time터미널에 쓸 수 있습니다 :

/usr/bin/time -o /dev/tty mycommand >/dev/null 2>/dev/null

time표준 오류로 출력을 유지하려면 추가 레벨의 파일 디스크립터 셔플 링이 필요합니다.

/usr/bin/time -o /dev/fd/3 mycommand 3>&2 >/dev/null 2>/dev/null

어떤으로 time유틸리티, 당신은 원하는 리디렉션을 수행하는 중간 쉘을 호출 할 수 있습니다. cd, 재 지정 등과 같은 추가 작업을 수행하기 위해 중간 셸을 호출하는 것은 매우 일반적입니다. 셸이 설계 한 작은 일입니다.

/usr/bin/time sh -c 'exec mycommand >/dev/null 2>/dev/null'


답변

[artur@asus-ux21e ~]$ find /etc/pki/CA/private/
/etc/pki/CA/private/
find: ‘/etc/pki/CA/private/’: Permission denied
[artur@asus-ux21e ~]$ time (find /etc/pki/CA/private/ &> /dev/null)

real    0m0.006s
user    0m0.001s
sys 0m0.004s


답변