Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Need help on perl Expect Module

by sriram83.life (Acolyte)
on Apr 23, 2014 at 04:55 UTC ( [id://1083273]=perlquestion: print w/replies, xml ) Need Help??

sriram83.life has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I want to write a script that extracts files in directories and their subdirectories recursively.I have used File::Find module to use wanted() subroutine to extract files recursively.While it recursively extracts,some duplicate files are appearing and asking for prompt to replace or rename. Here is the example prompt.

replace META-INF/MANIFEST.MF? [y]es, [n]o, [A]ll, [N]one, [r]ename:

Every time it appears,script has to send "A" reply to the prompt and it has to continue as i need to extract hundreds of files and get the required file.Can any one give me suggestions on how to write this using perl Expect?

Here is my code which uses File::Find module to find files recursively and perform some actions.

find( { wanted => \&wanted, }, @dirs); sub wanted { my $depth = $File::Find::dir =~ tr[/][]; return if $depth < $min_depth; if ( $File::Find::name =~ m/.zip$|file.*?$/ ) { print "I am extracting zip file : $File::Find::name\n"; &execute_command("unzip $File::Find::name"); #At this point it asks for prompt,i need to make it accept reply +automatically. #execute_command is a subroutine written in another module which +uses fork and exec to execute a shell command. elsif ( $File::Find::name =~ m/.gz$/ ) { &execute_command("gunzip $File::Find::name"); } }

Your help is much appreciated.

Replies are listed 'Best First'.
Re: Need help on perl Expect Module
by atcroft (Abbot) on Apr 23, 2014 at 05:11 UTC

    The Synopsis section of the Expect docs seems fairly straight-forward. While I haven't tested the code below, I believe it should work as intended.

    Hope that helps.

Re: Need help on perl Expect Module
by kcott (Archbishop) on Apr 23, 2014 at 05:36 UTC

    G'day sriram83.life,

    Without knowing anything about execute_command(), or the module where it is defined, any answer will involve a lot of guesswork; however, my gut feeling is that Expect is unnecessary.

    If the module is a core or CPAN module, provide a link; otherwise, provide the code.

    Why are you circumventing execute_command()'s prototypes? Perhaps it's expecting an argument to do what you want. If you don't understand any of that, see perlsub.

    I'd probably just use appropriate options for unzip and gunzip such that those commands do what you want. (Options often vary between OSes: see the manpages on your OS to find the correct ones for you.)

    -- Ken

      Hi Ken,

      Here is my execute_command code which just executes a command in shell and returns its exit status.

      sub execute_command { my($command) = @_; return &execute_command_rc(0,$command); } #Same, but can specify a return code to check for success sub execute_command_rc { my $expectedrc = $_[0]; my $command = $_[1]; my $pid = &my_fork($command); my $returned_pid = waitpid($pid,0); my $status = $?; &log_and_exit("no child procs to collect?????",$failure) if ($returned_pid == -1); &log_and_exit("returned pid: $returned_pid should have been $pid",$f +ailure) if ($returned_pid != $pid); # tim thinks perl should have macros like WEXITSTATUS my $exit_value = $status >> 8; my $signal_num = $status & 127; my $dumped_core = $status & 128; &log_message("$command exited with status: $exit_value (instead of $ +expectedrc)") if ($exit_value != $expectedrc); &log_message("$command killed by signal: $signal_num") if ($signal_num); &log_message("$command dumped core") if ($dumped_core); # emulate bash behavior. if process is killed, then return (128 | si +gnal no.) return ($signal_num | 128) if ($signal_num); return $exit_value; }

      log_message and log_and_exit sub routines are just logging sub routines.They are not for this context.

      My main focus here is,to unzip continuously and recursively and replace duplicate files that get unzipped. Thanks for your suggestion Ken, unzip -o helps overwrite files with out prompt.

      --Sriram
Re: Need help on perl Expect Module
by salva (Canon) on Apr 23, 2014 at 08:10 UTC
    My version of unzip has a -o flag that suppresses all that asking.

      ++ but it appears to be gunzip so the flag would be -f. And for that matter also -r if it needs to be recursive. The point that it's an easier matter to call the command with helpful arguments than wrapping more complexity is a good one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1083273]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-18 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found