Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Execute command and return error

Hi,

How do I let perl execute a command and return a possible error?

RM file.txt - in shell this would return an error: No such file.
Perl however won't give that error... Is it possible to let perl display that error?

See, I want to start a program using perl, but now I have no idea whether it started or not.
And if It didn't, then i want to know why (error).

#!/usr/bin/perl
print "Content-type:text/html\n\n";
$res = `rm file.txt`;
print $res;
exit;

Thank you.
0
TonyJix
Asked:
TonyJix
1 Solution
 
TintinCommented:

To get the error, you need to capture stderr

$res = `rm file.txt 2>&1`;

However, usual way of running external commands where you don't need to capture the output is

system "rm file.txt" and die "rm failed $!\n";
0
 
Adam314Commented:
If you want to remove a file in perl, you should use the built-in command unlink, rather than a shell command.  It will return the number of files removed, and set $! to the error, if any:

unlink 'file.txt' or die "Could not remove file.txt: $!\n";
0
 
TonyJixAuthor Commented:
Alright... the remove thing was only an example though, but it's appreciated.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now