2011-08-01 4 views
2

pwd가 다른 예제와 다른 결과를 반환하는 이유는 무엇입니까? 여기 무슨 일 이니?Perl -e Test Oddity?

[[email protected]:~/perltests]> cat testopensimple.pl 
#!/usr/bin/perl 

use strict; 
use warnings; 

# |<command> for writing 
# <command>| for reading 

testopen("| pwd"); 
testopen("pwd |"); 
testopen("| hostname"); 
testopen("| cd"); 
testopen("| sh"); 
testopen("| sleep 2"); 

sub testopen { 
     my $command = shift; 

     print "Exists: " . (-e $command ? "true" : "false") . "\n"; 
     print "testopen($command)\n"; 
     eval { 
       open(my $fh, $command) or die "$! [email protected]"; 
       my $data = join('', <$fh>); 
       close($fh) or die "$! [email protected]"; 
       print $data . "\n"; 
     }; 
     if ([email protected]) { 
       print [email protected] . "\n"; 
     } 
} 

[[email protected]:~/perltests]> perl testopensimple.pl 
Exists: true 
testopen(| pwd) 
/home/me/perltests 

Exists: true 
testopen(pwd |) 
/home/me/perltests 

Exists: false 
testopen(| hostname) 
unixbox1 

Exists: false 
testopen(| cd) 

Exists: false 
testopen(| sh) 

Exists: false 
testopen(| sleep 2) 

업데이트 :

나는 그것이 내장 된 외부 명령의 문제에 대 쉘이 있다고 확신하고 있지 않다. netstat과 같은 명령을 사용해보십시오. pwd은 파이프가있는 기존 검사에서 true를 반환하는 유일한 명령입니다.

업데이트 2 : 내가 뭐하고 있었 테스트의 여러 반복을 통해

라는 파일 '| pwd '와'pwd | ' 결국 만들어졌다. 그것은 내가보고있는 것을 설명합니다. 감사.

답변

3

-e '| pwd'은 현재 디렉토리에 | pwd이라는 파일이있는 경우에만 true를 반환합니다.

-e 'pwd |'은 현재 디렉토리에 pwd |이라는 파일이있는 경우에만 true를 반환합니다.

$ perl -E'say -e "| pwd" ? 1 : 0' 
0 

$ touch '| pwd' 

$ perl -E'say -e "| pwd" ? 1 : 0' 
1 

$ perl -E'say -e "pwd |" ? 1 : 0' 
0 

$ touch 'pwd |' 

$ perl -E'say -e "pwd |" ? 1 : 0' 
1 
+0

여기서 일어난 것 같습니다. 또한 <(read) 및> (write) 인디케이터를 사용하여 open()을 사용하여 테스트를 수행하면서 '| pwd '와'pwd | ' 파일. 감사. – user255205

+0

@ user255205 : 3-arg를 열어 두 번째 arg로'| -' 또는'- |'를 사용하여 파이프를 수행 할 수 있습니다. http://perldoc.perl.org/perlopentut.html의 "fortune"예제를 참조하십시오. – ysth

관련 문제