harry1982 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

i have problem in calling a shell script with in my perl program, here is what i need

perl script should call shell script for which i use system,but shell script which gets called needs ctrl-c at the end to terminate

if i press ctrl-c , my main perl script is getting terminated.

pls suggest on above, i tried fork but not really familer with that if i can use.

pls see sample code below.

#!/usr/bin/perl use strict; use warnings; # i have skipped many steps here for ease system("a2interf -i $t_dir -B -L lance_sg11"); # above call needs ctrl-c to be terminated # i.e. script a2interf needs ctrl-c to terminate

system call should terminate with ctrl-c , how can i proceed with my perl program after system call

Replies are listed 'Best First'.
Re: Run Shell script followed by ctrl-c within perl
by BrowserUk (Patriarch) on Nov 25, 2009 at 04:52 UTC

    Try this

    perl -le'$SIG{INT}='ignore'; system "./loop.sh"; print "command return +ed"; sleep 5; print "exiting"'

    Loop.sh

    #!/bin/sh while true do time; sleep 1; done

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Run Shell script followed by ctrl-c within perl
by keszler (Priest) on Nov 25, 2009 at 04:16 UTC
    Ctrl-C sends SIGINT, i.e. is equivalent to
    $ kill -INT pid

    For a better way of handling external processes see IPC::Run. For example:

    my $h = harness \@cmd, \<<IN, \$out; blah IN run $h; $h->signal('INT'); $h->finish;
Re: Run Shell script followed by ctrl-c within perl
by colwellj (Monk) on Nov 25, 2009 at 04:15 UTC
    Harry,
    How do you know when to crtl-c the shell script?
    And can you change the shell script to finish gracefully instead?
    I don't think fork or system will help as they don't give you the PID of the called script, Fork gives you the PID of the child.
    You might try something like this;
    my $rc = fork; if($rc == 0){ #child system("run_shell_script"); }else{ #parent #decide when to kill shell script by killing child #not sure if this won't create a zombie shell script #and system dosen't return a PID kill 9, $rc; }

      My issues is, iam trying to automate a download process , in which iam supposed to call the script which i mentioned

      more information on shell script is as below

      it requires 2 inputs , so when i call this script, i need to the output in screen to key in the inputs

      regarding ctrl-c termination, it will display status at the end as download completed and remains idle, unless we press ctrl-c it wont come out from the script.

      i have no right to modify shell script, so iam thinking alternatives to be considered in perl

      if i fork a child process which can run shell script and upon clicking ctrl-c if child process terminates and returns to parent , it would solve my pupose , but iam not sure if this supports in perl

        If you want to automate the script then you want to steer AWAY from user input/output so you don't want to click anything.
        Look at passing the inputs to your shell script from perl.
        If you have a very interactive shell script you will need to capture the output of the script in perl and have a conversation with it.
        Have a look at backticks in the perl manual or qx//;
        As keszler said IPC::Run seems your best bet for an interactive relationship.
Re: Run Shell script followed by ctrl-c within perl
by ww (Archbishop) on Nov 25, 2009 at 04:36 UTC

    Frankly, I don't see enough information here to offer substantive advice... so let me raise some more questions:

    • Why are you calling the shell script from Perl? Does it return a value or values needed by the Perl script?
    • What do you expect to happen after the shell script terminates?
    • Have you read the documentation for system and exec? (and consider also the capabilities in IPC::Run as suggested by keszler.

    You also need to tell us the answers to colwellj's questions.

    Then, perhaps (likely) we can be of more direct assistance.

    Update: Oh, yes -- is there some reason why a direct approach -- translating your shell script to Perl (which can do almost anything your shell can) -- is not feasible?