Link to home
Start Free TrialLog in
Avatar of aja101498
aja101498

asked on

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
Avatar of mwillsher
mwillsher

(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)
Opps. Forgot to mention $? is the status code, $! is the OS error message.
Avatar of aja101498

ASKER

Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
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;
Avatar of ozo
if you want to get output of a command, use ``
ozo,

please explain. I don't know what you mean by "
ozo,

please explain. I don't know what you mean by "
not " ``
as in
$mesage=`overlay x $op $ifile1 $ifile2 $ofile`;
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
@ means that @comlist is an array
@commlist=("overlay", "x", $op, $ifile1, $ifile2, $ofile);
How are you sending any message to the home page?
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
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.
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


 
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 '
I used this ''
  $mesage='overlay x $op $ifile1 $ifile2 $ofile 2>&1' ;

Thanks alot
'' should be ``
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  
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?

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

After
  $message=`overlay x $op $ifile1 $ifile2 $ofile 2>&1` ;
is there anything in "$?"
how does overlay report success or failure
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.
sorry I didn't reject the answer !!!

I don't know how this happend
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.
mwillsher
=======

print (system(@commlist)) gives 0 when failing.
and another 0 when succeeding
Does that mean anything to you ?
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.

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.

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;

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.


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
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.
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"?

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
#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>;
ASKER CERTIFIED SOLUTION
Avatar of andersl
andersl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry andersel

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