Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: How to call Linux command sequentially

by mv.ashwin@gmail.com (Novice)
on Sep 13, 2011 at 08:42 UTC ( #925647=note: print w/replies, xml ) Need Help??


in reply to Re: How to call Linux command sequentially
in thread How to call Linux command sequentially

Sorry for the typos. Thanks for your reply Graff. But i was NOT able get the expected output either using system or exec. Please help. when i just enter this commands in linux it work but inside .pl file its not working. Is there anyway to check if the command is actually invoked. Do the new process get created when i invoke a command in .pl
  • Comment on Re^2: How to call Linux command sequentially

Replies are listed 'Best First'.
Re^3: How to call Linux command sequentially
by graff (Chancellor) on Sep 13, 2011 at 09:26 UTC
    If the two commands work okay when run directly at a shell prompt, but then they don't work when you run them from inside a perl script, it's either because you did something wrong in the perl script, or else there's a difference between the interactive shell environment and the environment of the sub-shell that gets run from inside the perl script.

    As Utilitarian said, it would help if you show your code, and also show the specific error message(s) or other evidence you have that shows exactly what "not working" really means.

    And really, if it's just a matter of making it easier to run two processes in sequence without having to wait, using perl for that is overkill. If you just save the two command lines (or a single conjoined command line as suggested in my first reply) as a plain text file, you can "run" that file as a shell script:

    source my_shell_script
    or
    . my_shell_script
    (note the line-initial period).
Re^3: How to call Linux command sequentially
by Utilitarian (Vicar) on Sep 13, 2011 at 08:56 UTC
    When you call exec a new process is forked and the parent quits, whereas when you call system the parent waits for the system call to complete. Using backticks `` or the qx is similar to a system call but the output is returned to the caller. eg.
    $ perl -Mstrict -e 'my @out =qx{ls $ENV{HOME}/tmp}; chomp @out; print join (", " , @out), "\n";' a.out, test.c, test.dat, test.pl
    Could you show us how you call the command within your Perl script?

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      OK there was syntax problem i was able to run the command. But here comes another problem. The first command here creates the an environment, in that environment i need to execute the second command. Here nither system nor exec will do these. Untill I manually exit from environment the second command is not running. Once i exit from environment the second executes. But i need the second command to be executed in environment which was set by first command. Is it possible to do so? Please help!!!!
        You could try something like:
        #!/usr/bin/perl use strict; my $shpid = open( SH, '|-', '/bin/bash' ) or die "Can't launch bash: $ +!\n"; my @command_list = ( "first_command args ...\n", "second_command args ...\n" ... ); for my $cmd ( @command_list ) { print SH $cmd; } print SH "exit\n"; waitpid( $shpid, 0 );
        If the list of commands actually includes things that change the shell environment for subsequent commands, that should work as intended.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-08-14 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (76 votes). Check out past polls.

    Notices?