Calling shell scripts or shell commands from Perl 
Author Message
 Calling shell scripts or shell commands from Perl

Hi,

: I know this is a newbie question, but I looked through the
: Perl manual and all I found was a reference to a "standard"
: Shell subroutine that I don't know where to get, which apparently
: lets you call shell commands.

: Would greatly appreciate someone letting me know how I can call
: a UNIX shell script in a Perl program...

        I wrote a little script to help me invoke shell programs, and
        capture their standard output.  Perhaps it's what you're
        looking for?

        An example of how I use it follows for organizing the output
        of the UNIX ps command into a tree format (who is whose parent
        process).

        Good luck.

     (           \|/ )
Don ( Sunshine  --O-- ) St. Denis
     (           /|\ )

***************************** ps, from my bin directory **********************
#!/usr/bin/perl -w

# ps - do a ps, and organize the data into decending, rooted trees

#interesting subroutines
sub getpid {                    # (str)

    substr($str, 8, 6);

Quote:
}

sub getppid {                   # (str)

    substr($str, 14, 6);

Quote:
}

# get some library help - to get standard output from UNIX executables
require "$ENV{'HOME'}/bin/shell.perl";




# execute the system ps command
$ps_out = &ps(*ps_params);
#print $ps_out;



#shuffle sort, to get the pids/ppids in correct order
foreach $ind (1..$#array) {
    $pid = &getpid($array[$ind]);
#    foreach $srt ($ind+1..$#array) {
    foreach $srt (1..$#array) {
        $ppid = &getppid($array[$srt]);
        if ($ppid eq $pid) {    # Exchange below
            $tmp = $array[$ind + 1];
            $array[$ind + 1] = $array[$srt];
            $array[$srt] = $tmp;
            last;
        }
    }

Quote:
}

print $array[0], "\n";
$indent="";
foreach $ind (1..$#array-1) {
    if (defined($array[$ind])) {
        print $array[$ind], "\n";
        if (&getpid($array[$ind]) eq &getppid($array[$ind + 1])) {
            $indent = $indent . "  ";
        } else {
            $indent = "";
        }
        substr($array[$ind + 1], 47, 0) = $indent;
    }
Quote:
}

print $array[$#array], "\n";

0;
***************************** shell.perl *************************************
#!/usr/bin/perl -w
# $Header: is_shell,v 1.1 95/02/04 10:41:06 dsd Exp $

# is_shell - handy shell like subroutines

# $Log: is_shell,v $
# Revision 1.1  95/02/04  10:41:06  10:41:06  dsd (Donald St.Denis)
# Initial revision
#
# Revision 1.1  95/02/04  10:19:40  10:19:40  dsd (Donald St.Denis)
# Initial revision
#

# Functions:

#       &init_is_shell($debug, FILE) - initialize if and only if you need debugging
#                                      support
#       &dirname($ffn) - get the directory name from a full path name.
#       &basename($ffn) - get the file name from a full path name.
#       &hostame - get the hostname for this puppy.

sub init_is_shell {             # (debug, FILE)
    package IS_Shell;

    $d = $debug;
    open(ERR, ">&FILE");

Quote:
}    

sub hostname {                  # ()
    package IS_Shell;

    &main'get_stdout_from_exe(*exec);

Quote:
}    

sub pwd {                  # ()
    package IS_Shell;

    &main'get_stdout_from_exe(*exec);

Quote:
}

sub ps {                  # (alist)
    package IS_Shell;



    &main'get_stdout_from_exe(*exec);

Quote:
}

sub dirname {                   # (f)
    package IS_Shell;

    print ERR "is_shell:&dirname got >>$f<< as a parameter\n" if $d;
    if ($f !~ s/^(.*\/).*$/\1/o) {
  print ERR "is_shell:&dirname returning >>./<<\n" if $d;
 return "./";
    }
    print ERR "is_shell:&dirname returning >>$f<<\n" if $d;
    $f;

Quote:
}

sub basename {                      # (f)
    package IS_Shell;

    print ERR "is_shell:&basename got $f as a parameter\n" if $d;
    $f =~ s/^.*\/(.*)$/\1/o;
    print ERR "is_shell:&basename returning $f\n" if $d;
    $f;

Quote:
}

sub basename_test {                # used to test basename function
    package IS_Shell;
    $ret = 0;
    if (&main'basename("test1") ne "test1") {
        $ret = 1;
    }
    if (&main'basename("/test2") ne "test2") {
     $ret = 1;
    }
    if (&main'basename("/xxx/test3") ne "test3") {
        $ret = 1;
    }
    if (&main'basename("/xxx/test4.c") ne "test4.c") {
    $ret = 1;
    }
    if (&main'basename("xxx/test5") ne "test5") {
        $ret = 1;
    }
    if (&main'basename("test6/xxx") ne "xxx") {
    $ret = 1;
    }
    if (&main'basename("/xxx/test.7") ne "test.7") {
        $ret = 1;
    }
    if ($ret) {
        print ERR "basename test failed\n";
    } else {
        print ERR "basename test passed\n";
    }

Quote:
}

sub dirname_test {               # used to test dirname function
    package IS_Shell;
    $ret = 0;
    if (&main'dirname("test1") ne "./") {
        $ret = 1;
    }
    if (&main'dirname("/test2") ne "/") {
       $ret = 1;
    }
    if (&main'dirname("/xxx/test3") ne "/xxx/") {
        $ret = 1;
    }
    if (&main'dirname("/xxx/test4.c") ne "/xxx/") {
        $ret = 1;
    }
    if (&main'dirname("xxx/test5") ne "xxx/") {
        $ret = 1;
    }
    if (&main'dirname("test6/xxx") ne "test6/") {
    $ret = 1;
    }
    if (&main'dirname("/xxx/test.7") ne "/xxx/") {
        $ret = 1;
    }
    if ($ret) {
        print ERR "dirname test failed\n";
    } else {
        print ERR "dirname test passed\n";
    }

Quote:
}

sub get_stdout_from_exe {                  # (exe) is a list
    package IS_Shell;

    open(CPOUT, ">&STDOUT");       # save this for later
    $tmpfile = "/usr/tmp/hostname$$";
    close(STDOUT);
    open(STDOUT, "> $tmpfile");

    close(STDOUT);
    open(STDOUT, ">&CPOUT");      # get it back
    close(CPOUT);
    open(IN, "< $tmpfile");
    $out = "";
    while (<IN>) {
        $out = "$out$_";
    }
    close(IN);
    unlink $tmpfile;
    $out;

Quote:
}    

sub whence {                    # (exe)

    foreach (split(":", $ENV{"PATH"})) {
        if (-x "$_/$exe") {
            return "$_/$exe";
        }
    }
    "";

Quote:
}      

1;


Mon, 29 Dec 1997 03:00:00 GMT  
 Calling shell scripts or shell commands from Perl

|I know this is a newbie question, but I looked through the
|Perl manual and all I found was a reference to a "standard"
|Shell subroutine that I don't know where to get, which apparently
|lets you call shell commands.
|
|Would greatly appreciate someone letting me know how I can call
|a UNIX shell script in a Perl program...
|
This can be used (or you can just system() to a existing shell script)

system <<EOF;

my commands

EOF

note, no values will be interpreted inside of the EOF segment. If you need to
interpret, use the system command individually.


        http://www.cs.buffalo.edu/~stoner



Mon, 29 Dec 1997 03:00:00 GMT  
 Calling shell scripts or shell commands from Perl
I know this is a newbie question, but I looked through the
Perl manual and all I found was a reference to a "standard"
Shell subroutine that I don't know where to get, which apparently
lets you call shell commands.

Would greatly appreciate someone letting me know how I can call
a UNIX shell script in a Perl program...

Regards,
Rohan.




Mon, 29 Dec 1997 03:00:00 GMT  
 Calling shell scripts or shell commands from Perl

Quote:



>: |I know this is a newbie question, but I looked through the
>: |Perl manual and all I found was a reference to a "standard"
>: |Shell subroutine that I don't know where to get, which apparently
>: |lets you call shell commands.
>: |

Rohan mentioned a passing reference to Shell...

I'd already noticed that the standard distribution of perl comes with
Shell.pm, which, although I can successfully place a call to 'use',
hasn't actually been much use to me...

i.e I can write 'use Shell.pm;' happily enough, but the actual code seems
to be uncommented and undocumented.

Anyone (maybe the author?) care to elaborate?

Bill



Mon, 12 Jan 1998 03:00:00 GMT  
 Calling shell scripts or shell commands from Perl



: |I know this is a newbie question, but I looked through the
: |Perl manual and all I found was a reference to a "standard"
: |Shell subroutine that I don't know where to get, which apparently
: |lets you call shell commands.
: |
: |Would greatly appreciate someone letting me know how I can call
: |a UNIX shell script in a Perl program...
: |
: This can be used (or you can just system() to a existing shell script)

: system <<EOF;

: my commands

: EOF

: note, no values will be interpreted inside of the EOF segment. If you need to
: interpret, use the system command individually.


:         http://www.cs.buffalo.edu/~stoner

You can execute shell scripts/commands from a perl script by doing the following

that is by simply enclosing the command in backsticks and escaping special

recursive find routine in perl.



Mon, 12 Jan 1998 03:00:00 GMT  
 Calling shell scripts or shell commands from Perl


Quote:




> >: |I know this is a newbie question, but I looked through the
> >: |Perl manual and all I found was a reference to a "standard"
> >: |Shell subroutine that I don't know where to get, which apparently
> >: |lets you call shell commands.

> Rohan mentioned a passing reference to Shell...

> I'd already noticed that the standard distribution of perl comes with
> Shell.pm, which, although I can successfully place a call to 'use',
> hasn't actually been much use to me...

> i.e I can write 'use Shell.pm;' happily enough, but the actual code seems
> to be uncommented and undocumented.

> Anyone (maybe the author?) care to elaborate?

That'll be Larry :-)

From memory it's something like

        use Shell qw(ls echo);

        echo "foo bar";
        ls -l;

basically the 'use' magically creates and imports functions which when
called invoke the correspondingly named shell commands (simplification).

I think Larry did it as a bit of fun when AUTOLOAD was first added to
the language.

Quote:
> Bill

Tim.


Fri, 16 Jan 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. shell script from within a shell script

2. Perl script calling shell script failing

3. command line perl prob (perl 5.004_01 in shell scripts)

4. Scheme Shell: Shell Scripting for the 90's

5. external shell commands with perl script

6. Executing a shell command within a perl script.

7. How to run shell commands from Perl script?

8. running linux shell commands from perl scripts....

9. parallel execution of shell commands in Perl script

10. How to run shell commands from Perl script?

11. external shell commands with perl script

12. Executing a shell command within a perl script.

 

 
Powered by phpBB® Forum Software