[prog] Perl *nix

k.clair kclair at karl.serve.com
Mon Oct 28 13:45:08 EST 2002


There are many many ways to execute system commands with perl.

There's one function, system(), which takes a list of arguments that
constitute a system command to run.  you can either just give it one
command in quotes, or you can break up the command into the command and
it's flags, etc.  examples:
system("/bin/ls -l");
system("/bin/ls","-l");

system() returns the exit status of the command run, i believe.  i don't
actually use system() much myself.

Then there are the backticks, ``.  If you put a system command inside of
backticks, perl executes the command and returns any of the output that
the command outputs, in an array, with each line of the system command
output being an array element.  so to get a directory listing you
could do:
@dir_list = `/bin/ls -l`;


also, you can run system commands as filehandles, using pipes. 

if you want to send something to a program, you would open a filehandle
with a pipe going into the program:

open (TO, "| /some/program");

and then you would just print to TO to send data to the program.

if you want to get the output from a program, you would place the pipe
after the program:

open (OUTPUT, "/bin/ls -l ./ |");
while (<OUTPUT>) {
	print;
}

the above would print the files in the current directory.
(note you don't need the \n when printing because it is preserved from ls).

hope that helps a little!
kristina

On Mon, Oct 28, 2002 at 04:16:13PM -0500, kansas_kennedy at phreaker.net wrote:
- Thanks for the help on 'the sleeping program'. It surely helped. And I am too 
- lazy to use 'use strict'. Damn!!!
- 
- Anyhow, I want to execute *nix commands through Perl programs. For instance, 
- ls, netstat etc. Like, ls something if found this then cp it to somewhere 
- etc. etc.
- 
- But I really haven't come accross tutorials as such anywhere so far. Would 
- anyone like to discuss a bit on this? Jacinta?
- 
- Regards, Kenny.
- __________________________________________________
- Do You Yahoo!?
- Everything you'll ever need on one web page
- from News and Sport to Email and Music Charts
- http://uk.my.yahoo.com
- _______________________________________________
- Programming mailing list
- Programming at linuxchix.org
- http://www.linuxchix.org/mailman/listinfo/programming



More information about the Programming mailing list