Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

exec & system

by dideod.yang (Sexton)
on Jul 21, 2018 at 03:04 UTC ( #1218982=perlquestion: print w/replies, xml ) Need Help??

dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I need your idea to achieve my script.. I understand difference between exec and system. LOCAL is my local command in my linux. My goal is when LOCAL command fail, then script try again command. How can I do that? below script, each case has limit point.. Please help me.
#LOCAL: local command #case 1 # when LOCAL failed script die exec "LOCAL"; print "success"; #case 2 #when LOCAL failed but print suceess system "LOCAL"; print "success";

Replies are listed 'Best First'.
Re: exec & system
by kevbot (Priest) on Jul 21, 2018 at 04:41 UTC
    Hello dideod.yang,

    I do not understand how exec can help here, since the documentation states that it never returns. The system command seems more appropriate. Here is an example to help illustrate how you can use the system command to execute your command and to check its exit status. When placed inside the while loop, you can keep executing the shell script until you get the successful exit status of 0. I made my own shell script to randomly generate an exit status of 0 or 1. I show that below as well.

    #!/usr/bin/env perl use strict; use warnings; my $status = 1; # Keep trying until the exit status is zero while( $status ){ $status = system "./"; print "Still trying\n"; sleep 1; } print "Success!\n"; exit;
    #!/usr/bin/env bash BINARY=2 T=1 number=$RANDOM let "number %= $BINARY" if [ "$number" -eq $T ] then exit 1; else exit 0; fi
    The code will take a random number of iterations to complete. The output will look something like this,
    Still trying Still trying Still trying Success!
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1218982]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-07-11 08:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found