Operating System - Linux
1820707 Members
2789 Online
109627 Solutions
New Discussion юеВ

Re: running a shell script from perl

 
SOLVED
Go to solution
rashmi R
Advisor

running a shell script from perl

I want to capture the output of a shell script in my perl program on HP UX 11.11.
I cannot use FTP.
14 REPLIES 14
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

Yes more ways.

# cat test.sh
ls muthu # error
hostname # log

# chmod u+x test.sh

# cat test.pl
#!/usr/bin/perl
system("./test.sh 1>/tmp/out.log 2>/tmp/err.log");

# perl test.pl

That is it. Change the test.sh to your our script.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: running a shell script from perl

Hi Rashmi,

You can "system" call that comes with perl.
Redirect STDOUT and STDERR wherever you want, just like,
system(" /tmp/tmp.sh 1>/tmp/out.log 2>/tmp/err.log");

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

Use qx[./test.sh 1>/tmp/test.log 2>&1]

or

`./test.sh 1>/tmp/test.log 2>&1`

to complete your requirement.

You can use open() in perl to get output from shell command or script execution as well.

open FD, |./script.sh

See this:
http://www.sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/pod/perlfunc/open.html

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

Example thread:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1002042

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: running a shell script from perl

Hi,

Check this as well,

http://www.tldp.org/LDP/abs/html/wrapper.html#BASHANDPERL

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: running a shell script from perl

rashmi R
Advisor

Re: running a shell script from perl

can u tell me how do i get... this

open FD, |./script.sh
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

Use like,

# cat test.sh
ls muthu
hostname

# perl -e ' open (FD,"|sh test.sh");'
muthu not found
# hostname

PS: You have asked the method which I never used before :). See that link which I sent in prev. thread.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

More better, use close (File descriptor) as like,

perl -e ' open (FD,"|sh test.sh");close (FD);'

--
Muthu
Easy to suggest when don't know about the problem!
rashmi R
Advisor

Re: running a shell script from perl

well, to be more precise. i need the output of a shell script into a array / hash in my perl script

my $sh = Shell->new;
%kp = $sh->(perl -e '\.test.sh') something like this ...
Muthukumar_5
Honored Contributor
Solution

Re: running a shell script from perl

To store into an array then,

# perl -e '@arr=`./test.sh 2>&1`;print $arr[0] . " " . $arr[1] . "\n";'

2>&1 used to redirect Stand Error mode messages to standard output mode to store those informations into perl array.

--
Muthu

Easy to suggest when don't know about the problem!
rashmi R
Advisor

Re: running a shell script from perl

thanks a lot muthu kumar and arun.
Muthukumar_5
Honored Contributor

Re: running a shell script from perl

kwel. Use this http://forums1.itrc.hp.com/service/forums/helptips.do?#33 to assign points.

--
Muthu
Easy to suggest when don't know about the problem!
James R. Ferguson
Acclaimed Contributor

Re: running a shell script from perl

Hi Rashmi:

If you want to run a program within a perl script and capture that program's output, don't use 'system()'. It doesn't provide the program's output.

Instead, a simple choice is to use a backtick syntax:

$output = `/pathto/program`;

You program will run and your perl script will dutifully wait until it's done, placing the program's output in the variable you choose.

Regards!

...JRF...