I want to redirect my command output (standard output and standard error) to test.log file with below code.
I am trying to check the exit status of my command by giving some invalid option (-force) to ls
use strict;
use warnings;
#system("ls 2>&1 | tee logfile");
#system("ls > lllog ");'
my $cmd = &exec_cmd("cd reg_folder && ls -force 2>&1 | tee test.log");
sub exec_cmd{
system(@_);
my $result = $?;
print "===result $result===\n";
if(($result) != 0) {
print "status $result\n";
exit;
}
return $result;
}
Output:-
ls: invalid option -- 'e'
Try `ls --help' for more information.
===result 0===
After running system command , I am expecting the exit status should be non-zero and it has to enter if block. But it is showing exit status as 0 though the command failed.
Thanks.