'Shell'에 해당되는 글 2건

  1. 2011.02.18 How to run a shell script from a Perl program?
  2. 2007.11.22 Shell script ref.

[출처 : http://www.perlmonks.org/ ]

Answer: How to run a shell script from a Perl program?
contributed by radiantmatrix

Perl offers three slightly different mechanisms for executing external commands:

  1. system
  2. exec
  3. Backticks or qx//
  1. Using system

    system($command, @arguments); # For example: system( "sh", "script.sh", "--help" ); system("sh script.sh --help");
    System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application.

    Read the documentation for system for the nuances of how various invocations are slightly different.

  2. Using exec

    This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more.

  3. Using backticks or qx//

    my $output = `script.sh --option`; my $output = qx/script.sh --option/;
    The backtick operator and it's equivalent qx// excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

Answer: How to run a shell script from a Perl program?
contributed by snapdragon

The way to do this is to use a systen call to run your script:


However I thought that I'd mention a couple of points to go with this. 

  1. Don't assume that your shell environment variables will be kept - a shell script that works fine on the command line may not work when run from Perl
  2. Be careful with your directories - try to make sure you're executing from the right place. It's often worthwhile to do a chdir to the right directory in Perl before calling your script. i.e.  chdir "/path/to/script/";

Good luck!
Answer: How to run a shell script from a Perl program?
contributed by Plankton

I like to do this sometimes ...

my $shell_out = <<`SHELL`; for fs in `/sbin/fdisk -l | grep -v swap | grep Linux | cut -d ' ' -f1 +` do /sbin/e2label \$fs | grep recovery > /dev/null 2>&1 && echo "Y +ES" && exit 0 done echo "NO" exit 0 SHELL
... to capture the stdout of the shell commands in a scalar var.
Answer: How to run a shell script from a Perl program?
contributed by ruzam

A slightly longer version using the exec method with forked pipes (and an optional timeout value).

# two parameters: # cmd - a command or reference to an array of command + arguments # timeout - number of seconds to wait (0 = forever) # returns: # cmd exit status (-1 if timed out) # cmd results (STDERR and STDOUT merged into an array ref) sub ExecCmd { my $cmd = shift || return(0, []); my $timeout = shift || 0; # opening a pipe creates a forked process my $pid = open(my $pipe, '-|'); return(-1, "Can't fork: $!") unless defined $pid; if ($pid) { # this code is running in the parent process my @result = (); if ($timeout) { my $failed = 1; eval { # set a signal to die if the timeout is reached local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; @result = <$pipe>; alarm 0; $failed = 0; }; return(-1, ['command timeout', @result]) if $failed; } else { @result = <$pipe>; } close($pipe); # return exit status, command output return ($? >> 8), \@result; } # this code is running in the forked child process { # skip warnings in this block no warnings; # redirect STDERR to STDOUT open(STDERR, '>&STDOUT'); # exec transfers control of the process # to the command ref($cmd) eq 'ARRAY' ? exec(@$cmd) : exec($cmd); } # this code will not execute unless exec fails! print "Can't exec @$cmd: $!"; exit 1; }
Answer: How to run a shell script from a Perl program?
contributed by DeadPoet

If you want the PID, STDOUT, and STDERR, use IPC::Open3.


use IPC::Open3; local (*IN, *OUT, *ERR); my $pid = eval{ open3( \*IN, \*OUT, \*ERR, 'my_command', 'my_command_args' ); }; if ($@) { warn ($@); } ## capture your output, if needed... ## wait on the pid... wait( $pid, 0 );
Posted by rookiejava
Shell script ref site.







'TIP & TALK > Linux' 카테고리의 다른 글

Linux File System Cache (pdflush)  (0) 2010.04.30
VI 명령어 모음  (0) 2008.06.18
Shell script ref.  (0) 2007.11.22
VI 단축키  (0) 2007.11.22
LANG  (0) 2007.11.09
RPM 다운로드  (0) 2007.11.09
Posted by rookiejava