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: 199
  • Last Modified:

How report error a/ unsuccessful SYSTEM command !

I have a perl script which contain many SYSTEM commands. some of these commands may not succeed and that depends on the arguments provided by the user. Is there a way to control this. I mean if the system command gives an error,a message should be displayed and the program should exit. Thanks
0
aja101498
Asked:
aja101498
  • 17
  • 10
  • 8
  • +1
1 Solution
 
mwillsherCommented:
(system(@commlist) == 0) or die "system @args failed: $? : $!\n";

If you want to actual return code, shift the return from system() by 8 bits right ($rc >>= 8) - the lower 8 bits are set if the process died from a signal (other than SIGINT or SIGQUIT which are blocked)
0
 
mwillsherCommented:
Opps. Forgot to mention $? is the status code, $! is the OS error message.
0
 
aja101498Author Commented:
Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aja101498Author Commented:
Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
0
 
aja101498Author Commented:
and what is (system(@commlist)
Commlist ? you mean overlay in my case. see below.
and how you put the command below & its arguments and the test all together

system "overlay", "x", $op, $ifile1, $ifile2, $ofile;
0
 
ozoCommented:
if you want to get output of a command, use ``
0
 
aja101498Author Commented:
ozo,

please explain. I don't know what you mean by "
0
 
aja101498Author Commented:
ozo,

please explain. I don't know what you mean by "
0
 
ozoCommented:
not " ``
as in
$mesage=`overlay x $op $ifile1 $ifile2 $ofile`;
0
 
aja101498Author Commented:
Ozo, I still don't know what you mean my questions are:

1: if there is an error from the line below, how to report that to the user in the home page?
2:  mwillsher proposed :
               (system(@commlist) == 0) or die "system @args failed: $? : $!\n";

what is @ ?
what is commlist?
where to put my arguments in hes example ?
how to send the error message to the home page ?
Thanks
0
 
ozoCommented:
@ means that @comlist is an array
@commlist=("overlay", "x", $op, $ifile1, $ifile2, $ofile);
How are you sending any message to the home page?
0
 
aja101498Author Commented:
The user does not see anything except the html forms in the web page, so if the command doesn't succeed (there is an error), the user should know when/why the error occured therefore he can choose different parameters. I hope I made this clear.

Acording to your previous message, my system command should be like this:

@commlist=("overlay", "x", $op, $ifile1, $ifile2, $ofile);
system(@commlist) == 0) or die "system @args failed: $? : $!\n";
and this should work. thanks
0
 
ozoCommented:
If the command doesn't succed,  it's exit status will be in $?
If you want to put that in your html form, you can put it there the same way you everything else there.
But you probably don't want the script itself to die.
I'd sugest instead calling overlay with:

  $mesage=`overlay x $op $ifile1 $ifile2 $ofile`

The exit status will still be in $? if you want to test it, or report it to the user,
and any error message that the command may have printed to STDOUT or STDERR will be in $message.
0
 
aja101498Author Commented:
I followed your what you've said:

I used the below statment but it didn't work.
>  $mesage=`overlay x $op $ifile1 $ifile2 $ofile`

When I printed $message, It printed :
overlay x $op $ifile1 $ifile2 $ofile


 
0
 
ozoCommented:
Did you use
  $mesage=`overlay x $op $ifile1 $ifile2 $ofile 2>&1` ;
or
  $mesage='overlay x $op $ifile1 $ifile2 $ofile 2>&1' ;
?
i.e., did you quote with ` or '
0
 
aja101498Author Commented:
I used this ''
  $mesage='overlay x $op $ifile1 $ifile2 $ofile 2>&1' ;

Thanks alot
0
 
ozoCommented:
'' should be ``
0
 
aja101498Author Commented:
mwillsher
============
(system(@commlist) == 0) or die "system @args failed: $? : $!\n";

this didn't work. It executes the program and no message displayed when there is an
error. It just beeps when there is an error.

Thanks

OZO
===========
$mesage=`overlay x $op $ifile1 $ifile2 $ofile 2>&1` ;

print $message;

nothing happend. It executes the command
 but no message is printed. Thanks  
0
 
mwillsherCommented:
Try
print (system(@commlist)). See what you get when the command quits. If you get different values when the program fails as to it succeeding you can do:

$res=system(@commlist))
if($res==0) {
      # All okay
} else {
      # Oh dear
}

--
I'm not suprised you can get OZO's answer to work: '$mesage'!='$message'
i.e. you've mispelt it :/
--
Also, What do you mean by an error? Does this error cause the program to exit with a return code reflecting the error?

0
 
mwillsherCommented:
Sorry I added an extra ) :
$res=system(@commlist); # instead of $res=system(@commlist));

0
 
ozoCommented:
After
  $message=`overlay x $op $ifile1 $ifile2 $ofile 2>&1` ;
is there anything in "$?"
how does overlay report success or failure
0
 
aja101498Author Commented:
mwillsher
==========
the error in mesage'!='$message is just in the posted message not in the program so be
surprise :).

OZ
===
nothing in "$?"  it doesn't print anything.

>how does overlay report success or failure
 
the overlay is a program that works from the dos prompt, if the arguments matche then it produce a file in the hard disk and that's what I need but if the arguments mismatch (they are supplied by a user) the program (overlay gives a message and of course it doesn't produce the file). That's it. Thanks to you all.
0
 
aja101498Author Commented:
sorry I didn't reject the answer !!!

I don't know how this happend
0
 
ozoCommented:
Hmm, I didn't notice you mentioning DOS before.
Which version of perl are you using?  I think some early ports may have mis-implemented `` or system

If overlay is running, I suppose you could see if it produced the file to check for an error.
0
 
aja101498Author Commented:
mwillsher
=======

print (system(@commlist)) gives 0 when failing.
and another 0 when succeeding
Does that mean anything to you ?
0
 
mwillsherCommented:
Hmm. I can resubmit it :)

As a post-condition is the creation of a file you could always check for the file:

if(-e $filename) {
      #Yippee!
}else{
      #boo :(
}

Make sure you include the full path in $filename.

0
 
mwillsherCommented:
The 0 means it's not return a result code: see ozo's answer...
If that's the case I'd use the file checking method above.

0
 
aja101498Author Commented:
It is perl 5.0 for DOS.

>If overlay is running, I suppose you could see if it produced >the file to check for an error.

What you mean if overlay is running ?
How to produce the file to check in my case ?
if you $ifile1, $ifile2, $ofile, they can be checked but the problem when I reach the following line.

 system "overlay","x", $op, $ifile1, $ifile2, $ofile;

0
 
mwillsherCommented:
answering for ozo:
if overlay runs and exits you can always check for the file you expect to be created.
As it would seem that your Perl interpreter doesn't support system calls correctly (or DOS doesn't return values) you're stuck with this method.
See my code about on how to do this.


0
 
aja101498Author Commented:
Dear  mwillsher
I understand this. The input file are all there. I can check if there are exist or -e but
the problem is this
during the executing of overlay some file can't be used. For example overlay file1,file2,ofile. In this case maybe file 2 is not compatible w/ file1 so overlay will give an error but still the file is there and has no problems. According to your last message, there is NO solution in DOS you mean ?

Thanks
0
 
mwillsherCommented:
Does the program have to succeed to create an output file? i.e. is an output file created when the program fails?
If so, see above, if not...
Dunno. If backticks (`),system, $? and $! aren't returning anything your stuck, unless someone else knows some other trick.
It may be worth checking CPAN to see if there is a newer version of Perl for DOS.
0
 
ozoCommented:
there's also fork;exec and open("command|"), but if qx and system aren't working, then they may not work either.

Didn't you say that when there's an error "overlay gives a message and of course it doesn't produce the file"?

0
 
aja101498Author Commented:
Hi ozo,

you want to say check for the file if it exist then report to the user ?. Actually that's the only possibility.

Thanks
0
 
ozoCommented:
#You might also want to try:
open(COMMAND,"overlay x $op $ifile1 $ifile2 $ofile|") or die "can't open pipe to overlay x $op $ifile1 $ifile2 $ofile| :$!";
$message = join'',<COMMAND>;
0
 
anderslCommented:
Hi,
use a simple test,
if(system(args)==0)#OK, next call
else push(@failedCommands,$?) # $? or/and a meaningfull comment

when you print the results, you can say

if(@failedCommands) print "parameter problems:",
  # deal with the problems...

Anders
0
 
aja101498Author Commented:
Sorry andersel

I just got your post today I will try it and getback to you. Thanks
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 17
  • 10
  • 8
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now