Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Run multiple shell commands on the same perl script

by Daral (Initiate)
on Oct 30, 2012 at 22:24 UTC ( #1001568=perlquestion: print w/replies, xml ) Need Help??
Daral has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Im trying to run multiple shell commands on the same perl script, Could you please help me to know how. When I run the script below, it only picks the first commands. Thanks for your time and your help. use strict; my $ls =exec ("ls"); my $con = exec("ifconfig");
  • Comment on Run multiple shell commands on the same perl script

Replies are listed 'Best First'.
Re: Run multiple shell commands on the same perl script
by 2teez (Vicar) on Oct 30, 2012 at 22:42 UTC

    You can do like so:

    use warnings; use strict; for my $cmd(qw(ls ipconfig)){ system("$cmd"); }
    1. Please, check this How do I post a question effectively?

    2. "..When I run the script below, it only picks the first commands.."
    3. Reason and Answer:
      "..The "exec" function executes a system command *and never returns*; use "system" instead of "exec" if you want it to return...."
      Check also exec and system
    4. I used ipconfig instead of the OP ifconfig because I tested this on window OS.
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Run multiple shell commands on the same perl script
by Athanasius (Chancellor) on Oct 31, 2012 at 02:20 UTC

    Hello Daral, and welcome to the Monastery!

    As 2teez explained, you can use system to run an external command from within a Perl script. But note that system returns the command’s exit status, not its output. If you want to capture the command’s output, use backticks (or the equivalent qx operator):

    #! perl use strict; use warnings; my $ls = `ls`; print "Output from ls:\n\n", $ls, "\n"; my $con = `ipconfig`; print "Output from ipconfig:\n\n", $con, "\n";

    Update: To capture both the output and the exit status, use the CPAN module Capture::Tiny.

    Hope that helps,

    Athanasius <°(((><contra mundum

      $? returns the result code from the last system() call or backtick operation, I believe, so you can both capture output and process result codes.


      #!/bin/perl # first command deliberately generates an error.... print `rubbish`; print "Rubbish Result: $?\n"; # this command should succeed print "ls Output:\n"; print `ls -l`; print "ls Result: $?\n";
      Rubbish Result: -1 ls Output: total 10 -rw-r--r-- 1 Userid Domain Users 1242 Oct 30 10:04 1.txt -rw-r--r-- 1 Userid Domain Users 1531 Oct 30 10:04 2.txt -rwxr-xr-x 1 Userid Domain Users 351 Oct 30 10:48 -rwxr-xr-x 1 Userid Domain Users 108 Oct 31 14:21 ls Result: 0
      Note the first output of $? is -1 (an error), whilst the second is 0 (success)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001568]
Approved by marto
[choroba]: Good morning!
[Corion]: I hope you had a good weekend!
[marto]: jetlag has really done a number on the kids, it's been a tough week
choroba played with the band on Saturday, so Sunday was very sleepy
[choroba]: managed to release Syntax::Construct with 5.28 support in the night, though
[Corion]: choroba: Whee ;)
[Corion]: marto: Ouch - I would've thought that kids adapt much better, but that's obviously not the case...
[marto]: well, their mother let them sleep till 15:00 & 12:00 last week, which didn't help them adjust :P
[Corion]: I was "productive" over the weekend in the sense that I revived my old "Perlmonks on SQLite" code, which likely means I can get a test instance back up running on my webhost. Small steps :)
[Corion]: marto: Ow, no, that doesn't help at all :)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2018-06-25 08:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.