Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get return code and output from command in Perl

I'm trying to write a utility that will go through a file that would look like this:

# Directory | file name | action | # of days without modification to the file for the command to take action
/work/test/|a*|delete|1
/work/test/|b*|compress|0
/work/test/|c*|compress|1

My script will go through the file deciding if, for example, there are files under /work/test/ that start with 'a' that haven't been modified in the last 1 days, and if so, it would delete them.

For this, I use the find command. Example:

my $command = "find " . $values[0] . $values[1] . " -mtime +" . $values[3] . " -delete ;\n";
system ($command);

But, I've been asked to retrieve the return code for each step to verify that the every step worked fine.

Now, I know that system() returns the return code, and the backticks return the output. But, how can I get both?

like image 546
coconut Avatar asked Oct 01 '15 14:10

coconut


People also ask

How do I find my return code in Perl?

Examining exit code in Perl If you happen to execute one perl script from another, for example using the system function, Perl has the same variable $? containing the exit code of the "other program". The call to system will return the exit code and it will be also saved in the $? variable of Perl.

How do you store the output of a command to a variable in Perl?

The easiest way is to use the `` feature in Perl. This will execute what is inside and return what was printed to stdout: my $pid = 5892; my $var = `top -H -p $pid -n 1 | grep myprocess | wc -l`; print "not = $var\n"; This should do it.

What is $@ Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.

What does $$ mean in Perl?

Normally the $$ is used to print the current process ID. print $$;


2 Answers

After backticks are run, the return code is available in $?.

$?

The status returned by the last pipe close, backtick (``) command, successful call to wait() or waitpid(), or from the system() operator. This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it).

$output = `$some_command`;
print "Output of $some_command was '$output'.\n";
print "Exit code of $some_command was $?\n";
like image 138
mob Avatar answered Oct 24 '22 17:10

mob


The universal solution for backticks, system(), etc is to use the ${^CHILD_ERROR_NATIVE} variable. See the perlvar perldoc: http://perldoc.perl.org/perlvar.html#%24%7b%5eCHILD_ERROR_NATIVE%7d

${^CHILD_ERROR_NATIVE} The native status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator. On POSIX-like systems this value can be decoded with the WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, WSTOPSIG and WIFCONTINUED functions provided by the POSIX module.

like image 29
Andrew Yochum Avatar answered Oct 24 '22 16:10

Andrew Yochum