Running multiple shell commands inside perl script

12,682

You need to do the same thing that you would in your shell. For your test command, it would look something like:

$cmd = "ls -l ; cd /home/xyz ; ls -l";
system($cmd);

Or better yet, as suggested by BadFileMagic:

$cmd = "ls -l ; cd /home/xyz && ls -l";

This way, the second ls is not executed if the cd fails.

Share:
12,682
TopCoder
Author by

TopCoder

Updated on June 04, 2022

Comments

  • TopCoder
    TopCoder almost 2 years

    I am using System() command to execute shell command from perl script but I need to run multiple commands one after another. How can we do that in one line.

    What I am doing currently is :

    $cmd = "ls -l cd /home/xyz ls -l" , 
    System($cmd)
    

    I am sure that single command works fine , Can someone let me know if this is right way to do? if not what is wrong here?

  • BadFileMagic
    BadFileMagic about 13 years
    it probably doesn't matter in such as simple example as this, but using && instead of ; is better when stringing commands together, especially of the execution of the next is dependent on successful completion of the command preceding it.