Dirk80 has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise monks,
Here a little code snippet which opens the command "dir" for reading and then prints the output of "dir".
#!/usr/bin/perl use strict; use warnings; open(my $CMD, "dir |") || die $!; while( <$CMD> ) { print "$_"; } close($CMD);
Now I tried the same, but with a not-existing command "dira".
#!/usr/bin/perl use strict; use warnings; open(my $CMD, "dira |") || die $!; while( <$CMD> ) { print "$_"; } close($CMD);
Of course my goal would it be that the open command dies the program because the command "dira" does not exist.
But I get here on my german computer the following output:
Der Befehl "dira" ist entweder falsch geschrieben oder konnte nicht gefunden werden.
So my question is, how can I check when I open a process, if the command really exists?
Of course I could check if the output is "Der Befehl ...". But this is then dependent of the language, the OS, ... . In other words this would be a very specific solution.
Another possibility would be to loop through all pathes in the environment variable PATH and then checking if the command exists.
Unfortunately I do not have more ideas. That's the reason why I ask you.
Thank you for your help.
Greetings,
Dirk
|
|---|