Home > Archive > PERL Beginners > March 2005 > output from system call
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
output from system call
|
|
| Geraldine 1 2005-03-08, 3:57 pm |
| Hi,
Is there a way to store the output of a system call on unix?
eg. system("date");
I like to store the date output to a variable. The only way I know of is to open and write the output to a file and then read in to a variable. Is there a simpler way?
Thanks in advance.
Geraldine
| |
| Ankur Gupta 2005-03-08, 3:57 pm |
| geraldine_1@comcast.net wrote:
>Hi,
>Is there a way to store the output of a system call on unix?
>
>eg. system("date");
>
use backticks...
$date = `date`;
Don't forget to chomp the $date variable as I guess you just want the date, not the newline character with it...
chomp($date);
| |
| mgoland@optonline.net 2005-03-08, 3:57 pm |
|
----- Original Message -----
From: geraldine_1@comcast.net
Date: Tuesday, March 8, 2005 11:29 am
Subject: output from system call
> Hi,
Hello,
> Is there a way to store the output of a system call on unix?
>
sure
> eg. system("date");
my $Date = system("date");
>
> I like to store the date output to a variable. The only way I
> know of is to open and write the output to a file and then read in
> to a variable. Is there a simpler way?
>
> Thanks in advance.
>
> Geraldine
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>
| |
| Manav Mathur 2005-03-08, 3:57 pm |
|
chomp($date=`date`) ; ##exploiting what Perl offers but C doesnt
-----Original Message-----
From: Ankur Gupta [mailto:Ankur.Gupta@synopsys.com]
Sent: Tuesday, March 08, 2005 10:03 PM
To: geraldine_1@comcast.net
Cc: beginners@perl.org
Subject: Re: output from system call
geraldine_1@comcast.net wrote:
>Hi,
>Is there a way to store the output of a system call on unix?
>
>eg. system("date");
>
use backticks...
$date = `date`;
Don't forget to chomp the $date variable as I guess you just want the date,
not the newline character with it...
chomp($date);
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
****************************************
*****************
Disclaimer:
The contents of this E-mail (including the contents of the enclosure(s) or attachment(s) if any) are privileged and confidential material of MBT and should not be disclosed to, used by or copied in any manner by anyone other than the intended addressee(s)
. In case you are not the desired addressee, you should delete this message and/or re-direct it to the sender. The views expressed in this E-mail message (including the enclosure(s) or attachment(s) if any) are those of the individual sender, except wh
ere the sender expressly, and with authority, states them to be the views of MBT.
This e-mail message including attachment/(s), if any, is believed to be free of any virus. However, it is the responsibility of the recipient to ensure that it is virus free and MBT is not responsible for any loss or damage arising in any way from its us
e
****************************************
*****************
| |
| Chris Devers 2005-03-08, 3:57 pm |
| On Tue, 8 Mar 2005, Ankur Gupta wrote:
> geraldine_1@comcast.net wrote:
>
> use backticks...
>
> $date = `date`;
This is, of course, exactly the wrong way to solve this problem.
Perl has date facilities built in -- so *use them*! :-)
$ perl -le '$now = scalar localtime time ; print $now'
Tue Mar 8 11:50:47 2005
$ date
Tue Mar 8 11:50:48 EST 2005
$
So, by default, you don't get the time zone the way the `date` command
does, but if you need that, it's not hard to extract.
See --
perldoc -f time
perldoc -f localtime
-- for details on the built in time handling functions.
If you want to get fancier, CPAN modules like Time::Local, Date::Calc,
etc.
http://search.cpan.org/~jhi/perl-5....b/Time/Local.pm
http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod
And so on.
There's a lot of thing that can be legitimately done by calling out to a
system command, but getting the date isn't really one of them :-)
--
Chris Devers
| |
| Wiggins d'Anconia 2005-03-08, 3:57 pm |
| geraldine_1@comcast.net wrote:
> Hi,
> Is there a way to store the output of a system call on unix?
>
> eg. system("date");
>
> I like to store the date output to a variable. The only way I know of is to open and write the output to a file and then read in to a variable. Is there a simpler way?
>
> Thanks in advance.
>
> Geraldine
>
As the others have mentioned you can use the backticks to store the
output. But please only use this method when it is absolutely necessary.
Perhaps you just gave an example, but in case not, you can get the
current date from Perl itself much easier and much more safely.
Shelling out to a command using system or backticks is a *last resort*.
If there is something you don't know how to do in Perl or if it can be
done in Perl ask first, use 'system' et al second.
You need to check the return value of system, and whether the program
executed at all, you should consider full paths, make sure you have
taint checks on, and be sure to read the documentation for the program
you are calling thoroughly to understand its interface. In general
shelling out is slower, more error prone, less secure, and when done
right takes longer to code.
http://danconia.org
| |
| Geraldine 1 2005-03-08, 3:57 pm |
| Thanks everyone for the input.
Getting the date output is much easier in perl than I originally thought.
thanks!
Geraldine
> On Tue, 8 Mar 2005, Ankur Gupta wrote:
>
>
> This is, of course, exactly the wrong way to solve this problem.
>
> Perl has date facilities built in -- so *use them*! :-)
>
> $ perl -le '$now = scalar localtime time ; print $now'
> Tue Mar 8 11:50:47 2005
> $ date
> Tue Mar 8 11:50:48 EST 2005
> $
>
> So, by default, you don't get the time zone the way the `date` command
> does, but if you need that, it's not hard to extract.
>
> See --
>
> perldoc -f time
> perldoc -f localtime
>
> -- for details on the built in time handling functions.
>
> If you want to get fancier, CPAN modules like Time::Local, Date::Calc,
> etc.
>
> http://search.cpan.org/~jhi/perl-5....b/Time/Local.pm
> http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod
>
> And so on.
>
> There's a lot of thing that can be legitimately done by calling out to a
> system command, but getting the date isn't really one of them :-)
>
>
>
> --
> Chris Devers
|
|
|
|
|