Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to call Linux command sequentially

by mv.ashwin@gmail.com (Novice)
on Sep 13, 2011 at 07:13 UTC ( [id://925636]=perlquestion: print w/replies, xml ) Need Help??

mv.ashwin@gmail.com has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to perl. I am using the below two commands for many times. So taught of putting it in a perl. I want to execute the first command & than second one. Usually first command will take a minute to run. Please help me with this it will help me to save some time :o) Thanks a bunch in advance!!!

/bin/toolsetup.pl -p coi -config d4 -t all -ov /nfs/home/akmvx/test -n + toptest touch file1
********************************************* Hurraahh!!!!!!! it worked...... Thanks a bunch!!! for all the who helped me in this. I appreciate it!! :o) *********************************************

Replies are listed 'Best First'.
Re: How to call Linux command sequentially
by graff (Chancellor) on Sep 13, 2011 at 08:11 UTC
    If the problem is that you are having to wait for the first command to finish before you run the second one, just put them both on a single command line, like this:
    /bin/toolsetup.pl -p coi -config d4 -t all -ov /nfs/home/akmvx/test -n + toptest; touch file1
    or like this:
    /bin/toolsetup.pl -p coi -config d4 -t all -ov /nfs/home/akmvx/test -n + toptest && touch file1
    In the first example "touch" will always run after the perl script finishes. In the second example, "touch" will only run if the perl script ends with a "0" exit status (i.e. didn't "die" with a non-zero error condition).
Re: How to call Linux command sequentially
by mv.ashwin@gmail.com (Novice) on Sep 13, 2011 at 08:41 UTC

    Thnaks for your reply Graff But i was 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

      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
        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).
        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."
Re: How to call Linux command sequentially
by JavaFan (Canon) on Sep 13, 2011 at 09:40 UTC
    What's the problem here? In the shell, you can just type the first command, and type the second before the first is finished. It'll be executed right after the first is finished.
      OK. Now i do not have any error messages. 1. Using first command as shown above i want to create an environment. I was able to create it from my .pl script its is perfectly fine. 2. Once the environment is created the perl script idles it will not run the next line untill i exit form the environment. 3. I want to run the commands in the environment which was created by first command. I tried using cojoined command or individually one after another. but the perl idles aftre finishing the first command it wont run the rest of the lines untill i exit from the created environment.

        You seem to have a different opinion on what "sequentially" means. The meaning I am aware of for "sequentially" means "one after another", which means that no two commands may be running at the same time.

        You seem to want to run two commands at the same time - first the command that sets up the environment and starts a new shell, and in that shell run another command.

        The solution of piping in the commands into that shell or the solution of retrieving the environment settings from that shell are both approaches that can help here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found