Why is this exists check returning different results for pwd versus the other examples? What’s happening here?
[me@unixbox1:~/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 "$! $@";
my $data = join('', <$fh>);
close($fh) or die "$! $@";
print $data . "\n";
};
if ($@) {
print $@ . "\n";
}
}
[me@unixbox1:~/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)
Update:
I’m not convinced that it’s a shell built-in vs external command issue. Try it with a command like netstat which is external. pwd is the only command I’ve found thus far that returns true on the exists check with pipes.
Update 2:
Through the several iterations of testing I was doing, files named ‘| pwd’ and ‘pwd |’ ended up being created. That explains what I’m seeing. Thanks.
-e '| pwd'will only return true if you have a file named| pwdin the current directory.-e 'pwd |'will only return true if you have a file namedpwd |in the current directory.