Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running UNIX commands from Perl

Status
Not open for further replies.

MJAZ

Programmer
Aug 1, 2006
73
US
How can I run UNIX commands from Perl and store the results? I have tried Google but it was unhelpful.
 
Code:
my @files = qx{ls -al};
print $_ foreach (@files);

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
so ls -al is the UNIX command? so can I just replace that with the command that I want?
 
Yes, but instead of asking why not just try it? (just curious) You can also use the more common backticks operator.
Code:
my @results = `your_unix_command`;

- Section "Quote and Quote-like Operators
 
Sorry. My server (the machine that will be using this) is a while away, and is hard to get to. It is also broken, so I can't try it. Thanks for all your help! [smile]
 
Yes. The 'lines' of output that are usually printed by the unix command will end up as elements in the array lvalue. Typically you'd use this for query-type commands, where you want to get the output into variables in your program.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
you can also back ticks `` or SYSTEM to run UNIX commands, depending on what outcome ur looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top