System command

V

Varun Jindal

hello,

i am using a unix shell command within my perl script using system
command.

lets say, i want to know what is hte system time (it is just an
example).

so i use the system command, but i want to put the output of the
command in a variable in the perl script.
how do i do it.

thanks in advance,

--Varun.
 
A

Anno Siegel

Varun Jindal said:
hello,

i am using a unix shell command within my perl script using system
command.

lets say, i want to know what is hte system time (it is just an
example).

so i use the system command, but i want to put the output of the
command in a variable in the perl script.
how do i do it.

Read the documentation of the function you are using, in particular
the third paragraph. perldoc -f system.

Anno
 
S

Sherm Pendley

Varun said:
so i use the system command, but i want to put the output of the
command in a variable in the perl script.

The docs for the system() function say, in part, "This is not what you
want to use to capture the output from a command, for that you should
use ..."

sherm--
 
L

Leon

Varun Jindal said:
hello,

i am using a unix shell command within my perl script using system
command.

lets say, i want to know what is hte system time (it is just an
example).

so i use the system command, but i want to put the output of the
command in a variable in the perl script.
how do i do it.

thanks in advance,

--Varun.
@Result = `date`;
 
T

Tony Curtis

On 30 Nov 2004 03:18:49 -0800,
I got it. Thanks for your help and prompt responses.

OK. Now you realize that if you want the system time, you'd
just say:

my $now = time;

For a formatted person-friendly "now", use strftime() from the
POSIX module:

use POSIX qw(strftime);
my $now = strftime('%Y-%d-%m %H:%M:%S', localtime);

No need here to bother a shell with anything.
 
B

Ben Morrow

Quoth Tony Curtis said:
OK. Now you realize that if you want the system time, you'd
just say:

my $now = time;

For a formatted person-friendly "now", use strftime() from the
POSIX module:

use POSIX qw(strftime);
my $now = strftime('%Y-%d-%m %H:%M:%S', localtime);

BZZT! Wrong.

POSIX explicitly provided the %c format to strftime, which is the
user's preferred representation.

(BTW, I presume %Y-%d-%m was a typo, and not related to the American
habit of writing dates as %m/%d/%y ? If it wasn't, that was a *very* bad
format to pick on...)

Ben
 
L

Leon

Jim Gibson said:
Why the array? On my system, `date` returns only one line, and I would
do:

Sorry, my mistake, just in habit of using it with system commands that
return more than one line of text as result (can't quite explain why I then
decide to choose `date` in the example:s).

So should obviously be $Result or @Result depending on expected output:)
 
U

Uri Guttman

L> Sorry, my mistake, just in habit of using it with system commands
L> that return more than one line of text as result (can't quite
L> explain why I then decide to choose `date` in the example:s).

huh? system commands don't return ANY lines of text.

L> So should obviously be $Result or @Result depending on expected output:)

no. it should be a scalar or an array depending on whether you want the
result of backticks to be a single string or split on newlines. the
actual command and its 'expected output' in the backticks has nothing to
do with it.

uri
 
S

Sherm Pendley

Tad said:
Please show a system() command that returns more than one line of text
as result, I have never seen one.

Uri said:
huh? system commands don't return ANY lines of text.

I think you guys missed the point. Leon isn't talking about Perl's
system() function here. He's referring to his earlier use of
"@result=`date`" instead of $result=`date`. Varun asked "why the
array?", since the date command produces only one line of output.

Given the context of his reply, it's pretty obvious that Leon is
referring to his habit of using arrays to store the result of system
commands such as 'ls' or 'ps', that produce multiple lines of output.

sherm--
 
U

Uri Guttman

SP> I think you guys missed the point. Leon isn't talking about Perl's
SP> system() function here. He's referring to his earlier use of
SP> "@result=`date`" instead of $result=`date`. Varun asked "why the
SP> array?", since the date command produces only one line of output.

SP> Given the context of his reply, it's pretty obvious that Leon is
SP> referring to his habit of using arrays to store the result of system
SP> commands such as 'ls' or 'ps', that produce multiple lines of output.

i got that point. you should have quoted the second part of my post
where i point out that context determines what happens with backtick
output. and 'ls' and 'ps' have never been called system commands in my
experience. call plain programs or whatever but not system commands.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top