Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: exec & system

by kevbot (Priest)
on Jul 21, 2018 at 04:41 UTC ( #1218987=note: print w/replies, xml ) Need Help??


in reply to exec & system

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 rand_exit.sh until the exit status is zero while( $status ){ $status = system "./rand_exit.sh"; print "Still trying\n"; sleep 1; } print "Success!\n"; exit;
rand_exit.sh
#!/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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1218987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2019-05-21 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (129 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!