Solved

How  report  error a/ unsuccessful SYSTEM command !

Posted on 1998-10-16
36
187 Views
Last Modified: 2010-03-05
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
Comment
Question by:aja101498
  • 17
  • 10
  • 8
  • +1
36 Comments
 

Expert Comment

by:mwillsher
ID: 1205408
(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
 

Expert Comment

by:mwillsher
ID: 1205409
Opps. Forgot to mention $? is the status code, $! is the OS error message.
0
 

Author Comment

by:aja101498
ID: 1205410
Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
0
 

Author Comment

by:aja101498
ID: 1205411
Ok but how the OS message will be displayed in a home page for the user. Is that feasible. Thanks in advance.
0
 

Author Comment

by:aja101498
ID: 1205412
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205413
if you want to get output of a command, use ``
0
 

Author Comment

by:aja101498
ID: 1205414
ozo,

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

Author Comment

by:aja101498
ID: 1205415
ozo,

please explain. I don't know what you mean by "
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205416
not " ``
as in
$mesage=`overlay x $op $ifile1 $ifile2 $ofile`;
0
 

Author Comment

by:aja101498
ID: 1205417
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205418
@ means that @comlist is an array
@commlist=("overlay", "x", $op, $ifile1, $ifile2, $ofile);
How are you sending any message to the home page?
0
 

Author Comment

by:aja101498
ID: 1205419
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205420
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
 

Author Comment

by:aja101498
ID: 1205421
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205422
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
 

Author Comment

by:aja101498
ID: 1205423
I used this ''
  $mesage='overlay x $op $ifile1 $ifile2 $ofile 2>&1' ;

Thanks alot
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205424
'' should be ``
0
 

Author Comment

by:aja101498
ID: 1205425
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Expert Comment

by:mwillsher
ID: 1205426
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
 

Expert Comment

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

0
 
LVL 84

Expert Comment

by:ozo
ID: 1205428
After
  $message=`overlay x $op $ifile1 $ifile2 $ofile 2>&1` ;
is there anything in "$?"
how does overlay report success or failure
0
 

Author Comment

by:aja101498
ID: 1205429
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
 

Author Comment

by:aja101498
ID: 1205430
sorry I didn't reject the answer !!!

I don't know how this happend
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205431
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
 

Author Comment

by:aja101498
ID: 1205432
mwillsher
=======

print (system(@commlist)) gives 0 when failing.
and another 0 when succeeding
Does that mean anything to you ?
0
 

Expert Comment

by:mwillsher
ID: 1205433
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
 

Expert Comment

by:mwillsher
ID: 1205434
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
 

Author Comment

by:aja101498
ID: 1205435
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
 

Expert Comment

by:mwillsher
ID: 1205436
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
 

Author Comment

by:aja101498
ID: 1205437
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
 

Expert Comment

by:mwillsher
ID: 1205438
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205439
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
 

Author Comment

by:aja101498
ID: 1205440
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
 
LVL 84

Expert Comment

by:ozo
ID: 1205441
#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
 

Accepted Solution

by:
andersl earned 100 total points
ID: 1205442
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
 

Author Comment

by:aja101498
ID: 1205443
Sorry andersel

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now